-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor test for templatize ev2 package
* `PrecompilePipelineForEV2` renamed to `PrecompilePipelineFileForEV2` * `PrecompilePipelineForEV2` reintroduced but returns a parsed `pipeline.Pipeline` struct * refactoring for testability Signed-off-by: Gerd Oberlechner <[email protected]>
- Loading branch information
Showing
12 changed files
with
200 additions
and
82 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,42 @@ | ||
package ev2 | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"gopkg.in/yaml.v3" | ||
|
||
"github.com/Azure/ARO-HCP/tooling/templatize/internal/testutil" | ||
"github.com/Azure/ARO-HCP/tooling/templatize/pkg/config" | ||
"github.com/Azure/ARO-HCP/tooling/templatize/pkg/pipeline" | ||
) | ||
|
||
func TestPrecompilePipelineForEV2(t *testing.T) { | ||
defer func() { | ||
_ = os.Remove("../../testdata/ev2-precompiled-pipeline.yaml") | ||
_ = os.Remove("../../testdata/ev2-precompiled-test.bicepparam") | ||
}() | ||
|
||
func TestProcessPipelineForEV2(t *testing.T) { | ||
configProvider := config.NewConfigProvider("../../testdata/config.yaml") | ||
vars, err := configProvider.GetVariables("public", "int", "", newEv2ConfigReplacements()) | ||
vars, err := configProvider.GetVariables("public", "int", "", NewEv2ConfigReplacements()) | ||
if err != nil { | ||
t.Errorf("failed to get variables: %v", err) | ||
} | ||
newPipelinePath, err := PrecompilePipelineForEV2("../../testdata/pipeline.yaml", vars) | ||
originalPipeline, err := pipeline.NewPipelineFromFile("../../testdata/pipeline.yaml", vars) | ||
if err != nil { | ||
t.Errorf("failed to read new pipeline: %v", err) | ||
} | ||
files := make(map[string][]byte) | ||
files["test.bicepparam"] = []byte("param regionRG = '{{ .regionRG }}'") | ||
|
||
newPipeline, newFiles, err := processPipelineForEV2(originalPipeline, files, vars) | ||
if err != nil { | ||
t.Errorf("failed to precompile pipeline: %v", err) | ||
} | ||
|
||
p, err := pipeline.NewPipelineFromFile(newPipelinePath, vars) | ||
// verify pipeline | ||
pipelineContent, err := yaml.Marshal(newPipeline) | ||
if err != nil { | ||
t.Errorf("failed to read new pipeline: %v", err) | ||
t.Errorf("failed to marshal processed pipeline: %v", err) | ||
} | ||
fmt.Println(p) | ||
expectedParamsPath := "ev2-precompiled-test.bicepparam" | ||
if p.ResourceGroups[0].Steps[1].Parameters != expectedParamsPath { | ||
t.Errorf("expected parameters path %v, but got %v", expectedParamsPath, p.ResourceGroups[0].Steps[1].Parameters) | ||
testutil.CompareWithFixture(t, pipelineContent, testutil.WithExtension("pipeline.yaml")) | ||
|
||
// verify referenced files | ||
for filePath, content := range newFiles { | ||
testutil.CompareWithFixture(t, content, testutil.WithExtension(filePath)) | ||
} | ||
// TODO improve test, check against fixture | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package pipeline | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
func (p *Pipeline) DeepCopy(newPipelineFilePath string) (*Pipeline, error) { | ||
copy := new(Pipeline) | ||
data, err := yaml.Marshal(p) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to marshal pipeline: %v", err) | ||
} | ||
err = yaml.Unmarshal(data, copy) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to unmarshal pipeline: %v", err) | ||
} | ||
copy.pipelineFilePath = newPipelineFilePath | ||
return copy, nil | ||
} | ||
|
||
func (p *Pipeline) PipelineFilePath() string { | ||
return p.pipelineFilePath | ||
} | ||
|
||
func (p *Pipeline) EnterPipelineDir() (string, func(), error) { | ||
currentDir, err := os.Getwd() | ||
if err != nil { | ||
return "", nil, err | ||
} | ||
|
||
pipelineDir, err := filepath.Abs(filepath.Dir(p.pipelineFilePath)) | ||
if err != nil { | ||
return "", nil, err | ||
} | ||
err = os.Chdir(pipelineDir) | ||
if err != nil { | ||
return "", nil, err | ||
} | ||
|
||
return pipelineDir, func() { | ||
_ = os.Chdir(currentDir) | ||
}, 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
Oops, something went wrong.