-
Notifications
You must be signed in to change notification settings - Fork 466
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add test yaml config * switch to test configs in test dirs * add skip methods to blueprint test * embed BlueprintTestConfig in BlueprintTests
- Loading branch information
1 parent
2e48b63
commit fe03487
Showing
5 changed files
with
202 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package discovery | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"sigs.k8s.io/kustomize/kyaml/yaml" | ||
) | ||
|
||
const ( | ||
DefaultTestConfigFilename = "test.yaml" | ||
blueprintTestKind = "BlueprintTest" | ||
blueprintTestAPIVersion = "blueprints.cloud.google.com/v1alpha1" | ||
) | ||
|
||
type BlueprintTestConfig struct { | ||
yaml.ResourceMeta `json:",inline" yaml:",inline"` | ||
Spec struct { | ||
Skip bool `json:"skip" yaml:"skip"` | ||
} `json:"spec" yaml:"spec"` | ||
Path string | ||
} | ||
|
||
// GetTestConfig returns BlueprintTestConfig if found | ||
func GetTestConfig(path string) (BlueprintTestConfig, error) { | ||
_, err := os.Stat(path) | ||
if os.IsNotExist(err) { | ||
// BlueprintTestConfig does not exist, so we return an equivalent empty config | ||
emptyCfg := BlueprintTestConfig{} | ||
emptyCfg.Spec.Skip = false | ||
return emptyCfg, nil | ||
} | ||
data, err := os.ReadFile(path) | ||
if err != nil { | ||
return BlueprintTestConfig{}, fmt.Errorf("error reading %s: %v", path, err) | ||
} | ||
var bpc BlueprintTestConfig | ||
err = yaml.Unmarshal(data, &bpc) | ||
if err != nil { | ||
return BlueprintTestConfig{}, fmt.Errorf("error unmarshalling %s: %v", data, err) | ||
} | ||
bpc.Path = path | ||
err = isValidTestConfig(bpc) | ||
if err != nil { | ||
return BlueprintTestConfig{}, fmt.Errorf("error validating testconfig in %s: %v", path, err) | ||
} | ||
return bpc, nil | ||
} | ||
|
||
// isValidTestConfig validates a given BlueprintTestConfig | ||
func isValidTestConfig(b BlueprintTestConfig) error { | ||
if b.APIVersion != blueprintTestAPIVersion { | ||
return fmt.Errorf("invalid APIVersion %s expected %s", b.APIVersion, blueprintTestAPIVersion) | ||
} | ||
if b.Kind != blueprintTestKind { | ||
return fmt.Errorf("invalid Kind %s expected %s", b.Kind, blueprintTestKind) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package discovery | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_ShouldSkipTest(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
testCfg string | ||
shouldSkip bool | ||
errMsg string | ||
}{ | ||
{ | ||
name: "simple", | ||
testCfg: `apiVersion: blueprints.cloud.google.com/v1alpha1 | ||
kind: BlueprintTest | ||
metadata: | ||
name: test | ||
spec: | ||
skip: true | ||
`, | ||
shouldSkip: true, | ||
}, | ||
{ | ||
name: "invalid", | ||
testCfg: `apiVersion: blueprints.cloud.google.com/v1alpha1 | ||
kind: foo | ||
metadata: | ||
name: test | ||
spec: | ||
skip: true | ||
`, | ||
errMsg: "invalid Kind foo expected BlueprintTest", | ||
}, | ||
{ | ||
name: "empty none skipped", | ||
shouldSkip: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
assert := assert.New(t) | ||
testCfgPath := "" | ||
if tt.testCfg != "" { | ||
testCfgPath = setupTestCfg(t, tt.testCfg) | ||
} | ||
defer os.RemoveAll(testCfgPath) | ||
bpTestCfg, err := GetTestConfig(testCfgPath) | ||
if tt.errMsg != "" { | ||
assert.NotNil(err) | ||
assert.Contains(err.Error(), tt.errMsg) | ||
} else { | ||
assert.NoError(err) | ||
assert.Equal(tt.shouldSkip, bpTestCfg.Spec.Skip) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func setupTestCfg(t *testing.T, data string) string { | ||
t.Helper() | ||
assert := assert.New(t) | ||
baseDir, err := ioutil.TempDir("", "") | ||
assert.NoError(err) | ||
fPath := path.Join(baseDir, "test.yaml") | ||
err = ioutil.WriteFile(fPath, []byte(data), 0644) | ||
assert.NoError(err) | ||
return fPath | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters