diff --git a/cmd/version.go b/cmd/version.go index 4e4eb88ed8..98e79c4a8b 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -46,6 +46,16 @@ func generateCmdOutput(out io.Writer, check bool) { fmt.Fprintln(out, "Build Hostname: "+version.Hostname) fmt.Fprintln(out, "Go Version: "+version.GoVersion) + var wasmAvailable string + + if version.WasmRuntimeAvailable { + wasmAvailable = "available" + } else { + wasmAvailable = "unavailable" + } + + fmt.Fprintln(out, "WebAssembly: "+wasmAvailable) + if check { err := checkOPAUpdate(out) if err != nil { diff --git a/cmd/version_test.go b/cmd/version_test.go index cb211d3d9e..aaf26e14e0 100644 --- a/cmd/version_test.go +++ b/cmd/version_test.go @@ -10,23 +10,27 @@ import ( "net/http" "net/http/httptest" "os" + "sort" + "strings" "github.com/open-policy-agent/opa/internal/report" - "github.com/open-policy-agent/opa/version" "testing" ) func TestGenerateCmdOutputDisableCheckFlag(t *testing.T) { var stdout bytes.Buffer - setTestVersion() generateCmdOutput(&stdout, false) - expected := getTestVersion() - if stdout.String() != expected { - t.Fatalf("Expected output:%v but got %v", expected, stdout.String()) - } + expectOutputKeys(t, stdout.String(), []string{ + "Version", + "Build Commit", + "Build Timestamp", + "Build Hostname", + "Go Version", + "WebAssembly", + }) } func TestGenerateCmdOutputWithCheckFlagNoError(t *testing.T) { @@ -40,13 +44,23 @@ func TestGenerateCmdOutputWithCheckFlagNoError(t *testing.T) { baseURL, teardown := getTestServer(exp, http.StatusOK) defer teardown() - banner := "Latest Upstream Version: 100.0.0\n" + - "Download: https://openpolicyagent.org/downloads/v100.0.0/opa_darwin_amd64\n" + - "Release Notes: https://github.com/open-policy-agent/opa/releases/tag/v100.0.0\n" + os.Setenv("OPA_TELEMETRY_SERVICE_URL", baseURL) - expected := getTestVersion() + banner + var stdout bytes.Buffer - testGenerateCmdOutput(t, baseURL, expected) + generateCmdOutput(&stdout, true) + + expectOutputKeys(t, stdout.String(), []string{ + "Version", + "Build Commit", + "Build Timestamp", + "Build Hostname", + "Go Version", + "WebAssembly", + "Latest Upstream Version", + "Release Notes", + "Download", + }) } func TestCheckOPAUpdateBadURL(t *testing.T) { @@ -59,35 +73,27 @@ func TestCheckOPAUpdateBadURL(t *testing.T) { } } -func testGenerateCmdOutput(t *testing.T, url, expected string) { +func expectOutputKeys(t *testing.T, stdout string, expectedKeys []string) { t.Helper() - os.Setenv("OPA_TELEMETRY_SERVICE_URL", url) + var gotKeys []string - var stdout bytes.Buffer - setTestVersion() - - generateCmdOutput(&stdout, true) - - if stdout.String() != expected { - t.Fatalf("Expected output:\"%v\" but got \"%v\"", expected, stdout.String()) + for _, line := range strings.Split(strings.Trim(stdout, "\n"), "\n") { + gotKeys = append(gotKeys, strings.Split(line, ":")[0]) } -} -func setTestVersion() { - version.Version = "v0.20.0" - version.Vcs = "12345" - version.Timestamp = "2020-05-14T06:22:38Z" - version.Hostname = "foo" - version.GoVersion = "1.14.7" -} + sort.Strings(expectedKeys) + sort.Strings(gotKeys) -func getTestVersion() string { - return "Version: v0.20.0\n" + - "Build Commit: 12345\n" + - "Build Timestamp: 2020-05-14T06:22:38Z\n" + - "Build Hostname: foo\n" + - "Go Version: 1.14.7\n" + if len(expectedKeys) != len(gotKeys) { + t.Fatalf("expected %v but got %v", expectedKeys, gotKeys) + } + + for i := range expectedKeys { + if expectedKeys[i] != gotKeys[i] { + t.Fatalf("expected %v but got %v", expectedKeys, gotKeys) + } + } } func getTestServer(update interface{}, statusCode int) (baseURL string, teardownFn func()) { diff --git a/resolver/wasm/nop.go b/resolver/wasm/nop.go new file mode 100644 index 0000000000..4601f76889 --- /dev/null +++ b/resolver/wasm/nop.go @@ -0,0 +1,39 @@ +// Copyright 2020 The OPA Authors. All rights reserved. +// Use of this source code is governed by an Apache2 +// license that can be found in the LICENSE file. + +// +build !cgo + +package wasm + +import ( + "context" + "errors" + + "github.com/open-policy-agent/opa/ast" + "github.com/open-policy-agent/opa/resolver" +) + +type Resolver struct { +} + +func (r *Resolver) Entrypoints() []ast.Ref { + panic("unreachable") +} + +func (r *Resolver) Close() { + panic("unreachable") +} + +func (r *Resolver) Eval(ctx context.Context, input resolver.Input) (resolver.Result, error) { + + panic("unreachable") +} + +func (r *Resolver) SetData(data interface{}) error { + panic("unreachable") +} + +func New(entrypoints []ast.Ref, policy []byte, data interface{}) (*Resolver, error) { + return nil, errors.New("WebAssembly runtime not supported in this build") +} diff --git a/resolver/wasm/wasm.go b/resolver/wasm/wasm.go index 486bc8a995..67e2dad790 100644 --- a/resolver/wasm/wasm.go +++ b/resolver/wasm/wasm.go @@ -2,6 +2,8 @@ // Use of this source code is governed by an Apache2 // license that can be found in the LICENSE file. +// +build cgo + package wasm import ( diff --git a/version/wasm.go b/version/wasm.go new file mode 100644 index 0000000000..39ffe23341 --- /dev/null +++ b/version/wasm.go @@ -0,0 +1,10 @@ +// Copyright 2020 The OPA Authors. All rights reserved. +// Use of this source code is governed by an Apache2 +// license that can be found in the LICENSE file. + +// +build cgo + +package version + +// WasmRuntimeAvailable indicates if a wasm runtime is available in this OPA. +const WasmRuntimeAvailable = true diff --git a/version/wasm_nop.go b/version/wasm_nop.go new file mode 100644 index 0000000000..c9617be6ea --- /dev/null +++ b/version/wasm_nop.go @@ -0,0 +1,10 @@ +// Copyright 2020 The OPA Authors. All rights reserved. +// Use of this source code is governed by an Apache2 +// license that can be found in the LICENSE file. + +// +build !cgo + +package version + +// WasmRuntimeAvailable indicates if a wasm runtime is available in this OPA. +const WasmRuntimeAvailable = false