Skip to content

Commit

Permalink
Add new command to generate output types automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
tjy9206 committed Sep 13, 2024
1 parent 4211be2 commit c46ec42
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 32 deletions.
7 changes: 1 addition & 6 deletions cli/bpmetadata/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,6 @@ func generateMetadataForBpPath(bpPath string) error {
return fmt.Errorf("error extracting output types for blueprint at path: %s. Details: %w", bpPath, err)
}

Log.Info("jieyutian - outputTypes:", outputTypes)

// Update the bpMetadata with the extracted output types
for i, output := range bpMetaObj.Spec.Interfaces.Outputs {
if outputType, ok := outputTypes[output.Name]; ok {
Expand Down Expand Up @@ -210,10 +208,7 @@ func extractOutputTypesFromState(bpPath string) (map[string]*structpb.Value, err
outputTypes := make(map[string]*structpb.Value)

// Construct the path to the test/setup directory
tfDir := filepath.Join(bpPath, "test", "setup")

// Create a fake testing.T for tft.NewTFBlueprintTest
// fakeT := &testing.T{}
tfDir := filepath.Join(bpPath)

root, _ := tft.NewTFBlueprintTestForNonTest(
tft.WithTFDir(tfDir), // Setup test at the blueprint path
Expand Down
28 changes: 2 additions & 26 deletions infra/blueprint-test/pkg/tft/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ func NewTFBlueprintTest(t testing.TB, opts ...tftOption) *TFBlueprintTest {
loadTFEnvVar(tft.tfEnvVars, tft.getTFOutputsAsInputs(outputs))
if credsEnc, exists := tft.tfEnvVars[fmt.Sprintf("TF_VAR_%s", setupKeyOutputName)]; tft.saKey == "" && exists {
if credDec, err := b64.StdEncoding.DecodeString(credsEnc); err == nil {
t.Logf("jieyutian - cred: %s", credDec)
gcloud.ActivateCredsAndEnvVars(tft.t, string(credDec))
} else {
tft.t.Fatalf("Unable to decode setup sa key: %v", err)
Expand All @@ -272,31 +273,6 @@ func NewTFBlueprintTest(t testing.TB, opts ...tftOption) *TFBlueprintTest {
return tft
}

// mockT implements the testing.TB interface with minimal functionality
type mockT struct {
*gotest.T
}

// Logf is required by testing.TB but does nothing in this mock
func (m *mockT) Logf(format string, args ...any) {
fmt.Printf("mockT.Logf called with format: %s, args: %v\n", format, args)
}

// Errorf is required by testing.TB but does nothing in this mock
func (m *mockT) Errorf(format string, args ...any) {
fmt.Printf("mockT.Errorf called with format: %s, args: %v\n", format, args)
}

// Fatal is required by testing.TB but does nothing in this mock
func (m *mockT) Fatal(args ...any) {
fmt.Printf("mockT.Fatal called with args: %v\n", args)
}

// Fatalf is required by testing.TB but does nothing in this mock
func (m *mockT) Fatalf(format string, args ...any) {
fmt.Printf("mockT.Fatalf called with format: %s, args: %v\n", format, args)
}

// TFBlueprintTestForNonTest encapsulates the necessary parts of TFBlueprintTest
// for use outside of a testing context.
type TFBlueprintTestForNonTest struct {
Expand All @@ -308,7 +284,7 @@ type TFBlueprintTestForNonTest struct {
// non-testing scenarios.
func NewTFBlueprintTestForNonTest(opts ...tftOption) (*TFBlueprintTestForNonTest, error) {
// Create a mock testing.T implementation
mockT := &mockT{T: &gotest.T{}}
mockT := &utils.MockT{T: &gotest.T{}}

// Create a custom logger with minimal configuration
customLogger := logger.Discard
Expand Down
49 changes: 49 additions & 0 deletions infra/blueprint-test/pkg/utils/mock_t.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package utils

import (
"fmt"
"testing"
)

// MockT implements the testing.TB interface with minimal functionality
type MockT struct {
*testing.T
}

// Logf is required by testing.TB but does nothing in this mock
func (m *MockT) Logf(format string, args ...any) {
fmt.Printf("MockT.Logf called with format: %s, args: %v\n", format, args)
}

// Errorf is required by testing.TB but does nothing in this mock
func (m *MockT) Errorf(format string, args ...any) {
fmt.Printf("MockT.Errorf called with format: %s, args: %v\n", format, args)
}

// Fatal is required by testing.TB but does nothing in this mock
func (m *MockT) Fatal(args ...any) {
fmt.Printf("MockT.Fatal called with args: %v\n", args)
panic(fmt.Sprint(args...)) // Call panic to stop execution
}

// Fatalf is required by testing.TB but does nothing in this mock
func (m *MockT) Fatalf(format string, args ...any) {
fmt.Printf("MockT.Fatalf called with format: %s, args: %v\n", format, args)
panic(fmt.Sprintf(format, args...)) // Call panic to stop execution
}
56 changes: 56 additions & 0 deletions infra/blueprint-test/pkg/utils/mock_t_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package utils_test

import (
"testing"

"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/utils"
)

func TestMockT(t *testing.T) {
mockT := &utils.MockT{T: t}

// Test Logf
mockT.Logf("test logf with %s", "argument")
// Check the output (you might need to capture it for a more robust test)

// Test Errorf
mockT.Errorf("test errorf with %d", 123)
// Check the output

// Test Fatal
// Note: Fatal will stop execution, so use a subtest or defer to recover
t.Run("Test Fatal", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("mockT.Fatal did not panic")
}
}()
mockT.Fatal("test fatal")
})

// Test Fatalf
t.Run("Test Fatalf", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("mockT.Fatalf did not panic")
}
}()
mockT.Fatalf("test fatalf with %s", "argument")
})
}

0 comments on commit c46ec42

Please sign in to comment.