Skip to content

Commit 25dc70f

Browse files
authored
Add testing for Go 1.20 (#3372)
* Add testing for Go 1.20 * Update changelog PR number * Cleanup instead of defer in beego test * Use os.TempDir instead of t.TempDir * Remove temp dir after replaceBeego * Unset the views path * Use custom filesystem for testing tmpls * Support windows filepath
1 parent c52452d commit 25dc70f

File tree

5 files changed

+67
-27
lines changed

5 files changed

+67
-27
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ jobs:
109109
compatibility-test:
110110
strategy:
111111
matrix:
112-
go-version: [1.19, 1.18]
112+
go-version: ["1.20", 1.19, 1.18]
113113
os: [ubuntu-latest, macos-latest, windows-latest]
114114
# GitHub Actions does not support arm* architectures on default
115115
# runners. It is possible to acomplish this with a self-hosted runner

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1111
### Added
1212

1313
- Add the new `go.opentelemetry.io/contrib/instrgen` package to provide auto-generated source code instrumentation. (#3068)
14+
- Support [Go 1.20]. (#3372)
1415

1516
### Changed
1617

@@ -694,3 +695,5 @@ First official tagged release of `contrib` repository.
694695
[0.8.0]: https://github.com/open-telemetry/opentelemetry-go-contrib/releases/tag/v0.8.0
695696
[0.7.0]: https://github.com/open-telemetry/opentelemetry-go-contrib/releases/tag/v0.7.0
696697
[0.6.1]: https://github.com/open-telemetry/opentelemetry-go-contrib/releases/tag/v0.6.1
698+
699+
[Go 1.20]: https://go.dev/doc/go1.20

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,19 @@ This project is tested on the following systems.
4747

4848
| OS | Go Version | Architecture |
4949
| ------- | ---------- | ------------ |
50+
| Ubuntu | 1.20 | amd64 |
5051
| Ubuntu | 1.19 | amd64 |
5152
| Ubuntu | 1.18 | amd64 |
53+
| Ubuntu | 1.20 | 386 |
5254
| Ubuntu | 1.19 | 386 |
5355
| Ubuntu | 1.18 | 386 |
56+
| MacOS | 1.20 | amd64 |
5457
| MacOS | 1.19 | amd64 |
5558
| MacOS | 1.18 | amd64 |
59+
| Windows | 1.20 | amd64 |
5660
| Windows | 1.19 | amd64 |
5761
| Windows | 1.18 | amd64 |
62+
| Windows | 1.20 | 386 |
5863
| Windows | 1.19 | 386 |
5964
| Windows | 1.18 | 386 |
6065

instrumentation/github.com/astaxie/beego/otelbeego/test/beego_test.go

+57-26
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"path/filepath"
2525
"strings"
2626
"testing"
27+
"time"
2728

2829
"go.opentelemetry.io/contrib/instrumentation/github.com/astaxie/beego/otelbeego"
2930
"go.opentelemetry.io/contrib/instrumentation/github.com/astaxie/beego/otelbeego/internal"
@@ -36,6 +37,7 @@ import (
3637

3738
"github.com/astaxie/beego"
3839
beegoCtx "github.com/astaxie/beego/context"
40+
assetfs "github.com/elazarl/go-bindata-assetfs"
3941
"github.com/stretchr/testify/assert"
4042
"github.com/stretchr/testify/require"
4143
)
@@ -225,28 +227,59 @@ func TestStatic(t *testing.T) {
225227
assertSpan(t, spans[0], tc)
226228
}
227229

228-
func TestRender(t *testing.T) {
229-
// Disable autorender to enable traced render
230-
beego.BConfig.WebConfig.AutoRender = false
231-
addTestRoutes(t)
232-
defer replaceBeego()
233-
htmlStr := "<!DOCTYPE html><html lang=\"en\">" +
234-
"<head><meta charset=\"UTF-8\"><title>Hello World</title></head>" +
235-
"<body>This is a template test. Hello {{.name}}</body></html>"
230+
var htmlStr = `<!DOCTYPE html>
231+
<html lang="en">
232+
<head>
233+
<meta charset="UTF-8">
234+
<title>Hello World</title>
235+
</head>
236+
<body>This is a template test. Hello {{.name}}</body>
237+
</html>
238+
`
236239

237-
// Create a temp directory to hold a view
238-
dir := t.TempDir()
239-
240-
// Create the view
241-
file, err := os.CreateTemp(dir, "*index.tpl")
242-
require.NoError(t, err)
243-
defer file.Close()
244-
_, err = file.WriteString(htmlStr)
245-
require.NoError(t, err)
246-
// Add path to view path
247-
require.NoError(t, beego.AddViewPath(dir))
248-
beego.SetViewsPath(dir)
249-
_, tplName = filepath.Split(file.Name())
240+
func TestRender(t *testing.T) {
241+
tplName = "index.tpl"
242+
beego.SetTemplateFSFunc(func() http.FileSystem {
243+
return &assetfs.AssetFS{
244+
Asset: func(path string) ([]byte, error) {
245+
if _, f := filepath.Split(path); f == tplName {
246+
return []byte(htmlStr), nil
247+
}
248+
return nil, os.ErrNotExist
249+
},
250+
AssetDir: func(path string) ([]string, error) {
251+
switch path {
252+
case "", `\`:
253+
return []string{tplName}, nil
254+
}
255+
return nil, os.ErrNotExist
256+
},
257+
AssetInfo: func(path string) (os.FileInfo, error) {
258+
if _, f := filepath.Split(path); f == tplName {
259+
return &assetfs.FakeFile{
260+
Path: path,
261+
Len: int64(len(htmlStr)),
262+
Timestamp: time.Now(),
263+
}, nil
264+
}
265+
return nil, os.ErrNotExist
266+
},
267+
}
268+
})
269+
viewPath := "/"
270+
require.NoError(t, beego.AddViewPath(viewPath))
271+
272+
ctrl := &testController{
273+
Controller: beego.Controller{
274+
ViewPath: viewPath,
275+
EnableRender: true,
276+
},
277+
T: t,
278+
}
279+
app := beego.NewApp()
280+
app.Handlers.Add("/template/render", ctrl, "get:TemplateRender")
281+
app.Handlers.Add("/template/renderstring", ctrl, "get:TemplateRenderString")
282+
app.Handlers.Add("/template/renderbytes", ctrl, "get:TemplateRenderBytes")
250283

251284
sr := tracetest.NewSpanRecorder()
252285
tracerProvider := trace.NewTracerProvider(trace.WithSpanProcessor(sr))
@@ -259,7 +292,7 @@ func TestRender(t *testing.T) {
259292
rr := httptest.NewRecorder()
260293
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("http://localhost/template%s", str), nil)
261294
require.NoError(t, err)
262-
mw(beego.BeeApp.Handlers).ServeHTTP(rr, req)
295+
mw(app.Handlers).ServeHTTP(rr, req)
263296
body, err := io.ReadAll(rr.Result().Body)
264297
require.Equal(t, strings.Replace(htmlStr, "{{.name}}", "test", 1), string(body))
265298
require.NoError(t, err)
@@ -269,16 +302,14 @@ func TestRender(t *testing.T) {
269302
require.Len(t, spans, 6) // 3 HTTP requests, each creating 2 spans
270303
for _, span := range spans {
271304
switch span.Name() {
272-
case "/template/render":
273-
case "/template/renderstring":
274-
case "/template/renderbytes":
305+
case "GET":
275306
continue
276307
case internal.RenderTemplateSpanName,
277308
internal.RenderStringSpanName,
278309
internal.RenderBytesSpanName:
279310
assert.Contains(t, span.Attributes(), internal.TemplateKey.String(tplName))
280311
default:
281-
t.Fatal("unexpected span name")
312+
t.Fatal("unexpected span name", span.Name())
282313
}
283314
}
284315
}

instrumentation/github.com/astaxie/beego/otelbeego/test/go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.18
44

55
require (
66
github.com/astaxie/beego v1.12.3
7+
github.com/elazarl/go-bindata-assetfs v1.0.0
78
github.com/stretchr/testify v1.8.1
89
go.opentelemetry.io/contrib/instrumentation/github.com/astaxie/beego/otelbeego v0.39.0
910
go.opentelemetry.io/contrib/propagators/b3 v1.14.0

0 commit comments

Comments
 (0)