From f35c59106f15d60a8f7f3fc18379769874d3fe45 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Mon, 7 Oct 2024 16:06:20 +0200 Subject: [PATCH 1/7] Assert SDK version is consistent in the CLI generation process --- .codegen.json | 3 +++ internal/build/info_test.go | 45 +++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/.codegen.json b/.codegen.json index 077d072bba..49d8bff9e8 100644 --- a/.codegen.json +++ b/.codegen.json @@ -9,6 +9,9 @@ ".codegen/lookup.go.tmpl": "bundle/config/variable/lookup.go" }, "toolchain": { + "pre_setup": [ + "go test -timeout 240s -run TestConsistentDatabricksSdkVersion github.com/databricks/cli/internal/build" + ], "required": ["go"], "post_generate": [ "go run ./bundle/internal/schema/*.go ./bundle/schema/jsonschema.json", diff --git a/internal/build/info_test.go b/internal/build/info_test.go index 1ae94fbce7..483c483e7b 100644 --- a/internal/build/info_test.go +++ b/internal/build/info_test.go @@ -1,9 +1,54 @@ package build import ( + "context" + "os" + "path/filepath" + "strings" "testing" + + "github.com/databricks/cli/libs/git" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "golang.org/x/mod/modfile" ) func TestGetDetails(t *testing.T) { GetInfo() } + +// This test is stored in the internal package to avoid build errors since this +// test itself is run during the CLI generation process. +func TestConsistentDatabricksSdkVersion(t *testing.T) { + // Read the go.mod file + b, err := os.ReadFile("../../go.mod") + require.NoError(t, err) + + // Parse the go.mod file to get the databricks-sdk version + modFile, err := modfile.Parse("../../go.mod", b, nil) + require.NoError(t, err) + + modulePath := "github.com/databricks/databricks-sdk-go" + var version string + for _, r := range modFile.Require { + if r.Mod.Path == modulePath { + version = r.Mod.Version + } + } + require.NotEmpty(t, version) + + // Clone the Databricks Go SDK to get the version of the OpenAPI spec used to + // generate the SDK. We cannot use go mod vendor to download the SDK because + // that command only downloads the necessary files and thus skips ".codegen". + tmpDir := t.TempDir() + err = git.Clone(context.Background(), "https://github.com/databricks/databricks-sdk-go.git", version, tmpDir) + require.NoError(t, err) + + cliSha, err := os.ReadFile("../../.codegen/_openapi_sha") + require.NoError(t, err) + + sdkSha, err := os.ReadFile(filepath.Join(tmpDir, ".codegen/_openapi_sha")) + require.NoError(t, err) + + assert.Equal(t, strings.TrimSpace(string(cliSha)), strings.TrimSpace(string(sdkSha)), "please update the SDK version before generating the CLI") +} From 808f6ff23c5776be32f4f6510c2036fe1a81f049 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Mon, 7 Oct 2024 16:12:51 +0200 Subject: [PATCH 2/7] comment --- internal/build/info_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/internal/build/info_test.go b/internal/build/info_test.go index 483c483e7b..f73ed87a63 100644 --- a/internal/build/info_test.go +++ b/internal/build/info_test.go @@ -19,6 +19,10 @@ func TestGetDetails(t *testing.T) { // This test is stored in the internal package to avoid build errors since this // test itself is run during the CLI generation process. +// This test ensures that the OpenAPI SHA the CLI is being generated from matches +// the OpenAPI SHA of the Go SDK version used in the CLI. We should always upgrade +// the Go SDK version before generating the CLI because downstream generated assets +// like the bundle schema depend on the Go SDK itself. func TestConsistentDatabricksSdkVersion(t *testing.T) { // Read the go.mod file b, err := os.ReadFile("../../go.mod") From 4f9f4afb522117107547bca46542f0e3f8f72252 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Mon, 7 Oct 2024 16:33:21 +0200 Subject: [PATCH 3/7] move assertion to post_generate --- .codegen.json | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.codegen.json b/.codegen.json index 49d8bff9e8..4524ab55dd 100644 --- a/.codegen.json +++ b/.codegen.json @@ -9,11 +9,9 @@ ".codegen/lookup.go.tmpl": "bundle/config/variable/lookup.go" }, "toolchain": { - "pre_setup": [ - "go test -timeout 240s -run TestConsistentDatabricksSdkVersion github.com/databricks/cli/internal/build" - ], "required": ["go"], "post_generate": [ + "go test -timeout 240s -run TestConsistentDatabricksSdkVersion github.com/databricks/cli/internal/build", "go run ./bundle/internal/schema/*.go ./bundle/schema/jsonschema.json", "echo 'bundle/internal/tf/schema/\\*.go linguist-generated=true' >> ./.gitattributes", "echo 'go.sum linguist-generated=true' >> ./.gitattributes", From 3cb5a21916c911c1c2d96da296b452e370def592 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Mon, 7 Oct 2024 17:01:02 +0200 Subject: [PATCH 4/7] make test work with specific commits as well --- internal/build/info_test.go | 42 ++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/internal/build/info_test.go b/internal/build/info_test.go index f73ed87a63..ad6e0f4b54 100644 --- a/internal/build/info_test.go +++ b/internal/build/info_test.go @@ -1,13 +1,16 @@ package build import ( - "context" + "encoding/json" + "fmt" + "io" + "net/http" "os" - "path/filepath" "strings" "testing" - "github.com/databricks/cli/libs/git" + "os/exec" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "golang.org/x/mod/modfile" @@ -41,17 +44,36 @@ func TestConsistentDatabricksSdkVersion(t *testing.T) { } require.NotEmpty(t, version) - // Clone the Databricks Go SDK to get the version of the OpenAPI spec used to - // generate the SDK. We cannot use go mod vendor to download the SDK because - // that command only downloads the necessary files and thus skips ".codegen". - tmpDir := t.TempDir() - err = git.Clone(context.Background(), "https://github.com/databricks/databricks-sdk-go.git", version, tmpDir) + // Full path of the package. For example: github.com/databricks/databricks-sdk-go@v0.47.1-0.20241002195128-6cecc224cbf7 + fullPath := fmt.Sprintf("%s@%s", modulePath, version) + + type goListResponse struct { + Origin struct { + Hash string `json:"Hash"` + } + } + + // Using the go CLI query for the git hash corresponding to the databricks-sdk-go version + cmd := exec.Command("go", "list", "-m", "-json", "-mod=readonly", fullPath) + out, err := cmd.Output() require.NoError(t, err) + parsedOutput := new(goListResponse) + err = json.Unmarshal(out, parsedOutput) + require.NoError(t, err) + hash := parsedOutput.Origin.Hash + require.NotEmpty(t, hash) - cliSha, err := os.ReadFile("../../.codegen/_openapi_sha") + // Read the OpenAPI SHA from the Go SDK. + url := fmt.Sprintf("https://raw.githubusercontent.com/databricks/databricks-sdk-go/%s/.codegen/_openapi_sha", hash) + resp, err := http.Get(url) require.NoError(t, err) + defer resp.Body.Close() + require.Equal(t, http.StatusOK, resp.StatusCode) - sdkSha, err := os.ReadFile(filepath.Join(tmpDir, ".codegen/_openapi_sha")) + sdkSha, err := io.ReadAll(resp.Body) + require.NoError(t, err) + + cliSha, err := os.ReadFile("../../.codegen/_openapi_sha") require.NoError(t, err) assert.Equal(t, strings.TrimSpace(string(cliSha)), strings.TrimSpace(string(sdkSha)), "please update the SDK version before generating the CLI") From b6bb5ca1238c07869838f7d3394804b00fc4fd90 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Mon, 7 Oct 2024 17:04:08 +0200 Subject: [PATCH 5/7] - --- internal/build/info_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/build/info_test.go b/internal/build/info_test.go index ad6e0f4b54..72b0ed6f17 100644 --- a/internal/build/info_test.go +++ b/internal/build/info_test.go @@ -49,7 +49,7 @@ func TestConsistentDatabricksSdkVersion(t *testing.T) { type goListResponse struct { Origin struct { - Hash string `json:"Hash"` + Hash string } } From 5c7753330df8de57ad578c91cd1ecdce7ba5f6ae Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Tue, 8 Oct 2024 18:06:17 +0200 Subject: [PATCH 6/7] - --- internal/build/info_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/internal/build/info_test.go b/internal/build/info_test.go index 72b0ed6f17..d862daae1d 100644 --- a/internal/build/info_test.go +++ b/internal/build/info_test.go @@ -20,8 +20,6 @@ func TestGetDetails(t *testing.T) { GetInfo() } -// This test is stored in the internal package to avoid build errors since this -// test itself is run during the CLI generation process. // This test ensures that the OpenAPI SHA the CLI is being generated from matches // the OpenAPI SHA of the Go SDK version used in the CLI. We should always upgrade // the Go SDK version before generating the CLI because downstream generated assets From 0773084da581778e7d94ec220867e7ef0bd32725 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Mon, 14 Oct 2024 18:11:48 +0200 Subject: [PATCH 7/7] move test --- internal/build/info_test.go | 69 ------------------------ internal/build/sdk_consistency_test.go | 73 ++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 69 deletions(-) create mode 100644 internal/build/sdk_consistency_test.go diff --git a/internal/build/info_test.go b/internal/build/info_test.go index d862daae1d..1ae94fbce7 100644 --- a/internal/build/info_test.go +++ b/internal/build/info_test.go @@ -1,78 +1,9 @@ package build import ( - "encoding/json" - "fmt" - "io" - "net/http" - "os" - "strings" "testing" - - "os/exec" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - "golang.org/x/mod/modfile" ) func TestGetDetails(t *testing.T) { GetInfo() } - -// This test ensures that the OpenAPI SHA the CLI is being generated from matches -// the OpenAPI SHA of the Go SDK version used in the CLI. We should always upgrade -// the Go SDK version before generating the CLI because downstream generated assets -// like the bundle schema depend on the Go SDK itself. -func TestConsistentDatabricksSdkVersion(t *testing.T) { - // Read the go.mod file - b, err := os.ReadFile("../../go.mod") - require.NoError(t, err) - - // Parse the go.mod file to get the databricks-sdk version - modFile, err := modfile.Parse("../../go.mod", b, nil) - require.NoError(t, err) - - modulePath := "github.com/databricks/databricks-sdk-go" - var version string - for _, r := range modFile.Require { - if r.Mod.Path == modulePath { - version = r.Mod.Version - } - } - require.NotEmpty(t, version) - - // Full path of the package. For example: github.com/databricks/databricks-sdk-go@v0.47.1-0.20241002195128-6cecc224cbf7 - fullPath := fmt.Sprintf("%s@%s", modulePath, version) - - type goListResponse struct { - Origin struct { - Hash string - } - } - - // Using the go CLI query for the git hash corresponding to the databricks-sdk-go version - cmd := exec.Command("go", "list", "-m", "-json", "-mod=readonly", fullPath) - out, err := cmd.Output() - require.NoError(t, err) - parsedOutput := new(goListResponse) - err = json.Unmarshal(out, parsedOutput) - require.NoError(t, err) - hash := parsedOutput.Origin.Hash - require.NotEmpty(t, hash) - - // Read the OpenAPI SHA from the Go SDK. - url := fmt.Sprintf("https://raw.githubusercontent.com/databricks/databricks-sdk-go/%s/.codegen/_openapi_sha", hash) - resp, err := http.Get(url) - require.NoError(t, err) - defer resp.Body.Close() - require.Equal(t, http.StatusOK, resp.StatusCode) - - sdkSha, err := io.ReadAll(resp.Body) - require.NoError(t, err) - - cliSha, err := os.ReadFile("../../.codegen/_openapi_sha") - require.NoError(t, err) - - assert.Equal(t, strings.TrimSpace(string(cliSha)), strings.TrimSpace(string(sdkSha)), "please update the SDK version before generating the CLI") -} diff --git a/internal/build/sdk_consistency_test.go b/internal/build/sdk_consistency_test.go new file mode 100644 index 0000000000..37f60250c0 --- /dev/null +++ b/internal/build/sdk_consistency_test.go @@ -0,0 +1,73 @@ +package build + +import ( + "encoding/json" + "fmt" + "io" + "net/http" + "os" + "os/exec" + "strings" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "golang.org/x/mod/modfile" +) + +// This test ensures that the OpenAPI SHA the CLI is being generated from matches +// the OpenAPI SHA of the Go SDK version used in the CLI. We should always upgrade +// the Go SDK version before generating the CLI because downstream generated assets +// like the bundle schema depend on the Go SDK itself. +func TestConsistentDatabricksSdkVersion(t *testing.T) { + // Read the go.mod file + b, err := os.ReadFile("../../go.mod") + require.NoError(t, err) + + // Parse the go.mod file to get the databricks-sdk version + modFile, err := modfile.Parse("../../go.mod", b, nil) + require.NoError(t, err) + + modulePath := "github.com/databricks/databricks-sdk-go" + var version string + for _, r := range modFile.Require { + if r.Mod.Path == modulePath { + version = r.Mod.Version + } + } + require.NotEmpty(t, version) + + // Full path of the package. For example: github.com/databricks/databricks-sdk-go@v0.47.1-0.20241002195128-6cecc224cbf7 + fullPath := fmt.Sprintf("%s@%s", modulePath, version) + + type goListResponse struct { + Origin struct { + Hash string + } + } + + // Using the go CLI query for the git hash corresponding to the databricks-sdk-go version + cmd := exec.Command("go", "list", "-m", "-json", "-mod=readonly", fullPath) + out, err := cmd.Output() + require.NoError(t, err) + parsedOutput := new(goListResponse) + err = json.Unmarshal(out, parsedOutput) + require.NoError(t, err) + hash := parsedOutput.Origin.Hash + require.NotEmpty(t, hash) + + // Read the OpenAPI SHA from the Go SDK. + url := fmt.Sprintf("https://raw.githubusercontent.com/databricks/databricks-sdk-go/%s/.codegen/_openapi_sha", hash) + resp, err := http.Get(url) + require.NoError(t, err) + defer resp.Body.Close() + require.Equal(t, http.StatusOK, resp.StatusCode) + + sdkSha, err := io.ReadAll(resp.Body) + require.NoError(t, err) + + cliSha, err := os.ReadFile("../../.codegen/_openapi_sha") + require.NoError(t, err) + + assert.Equal(t, strings.TrimSpace(string(cliSha)), strings.TrimSpace(string(sdkSha)), "please update the SDK version before generating the CLI") +}