package promote import ( "os" "path" "testing" "github.com/stretchr/testify/assert" ) var warehouseLockFile = "testdata/package_lock/warehouse-lock-pass.yaml" var workloadLockFile = "testdata/package_lock/workload-lock-pass.yaml" var ( stage1 = "ret-edge-stage1-foreman" stage2 = "ret-edge-stage2-foreman" ) func TestNewConfigForPackagePromotion(t *testing.T) { east2 := "us-east2" const napac = "napac" args := os.Args os.Args = []string{ PromoteTag, "-lockfile", workloadLockFile, "-src-project", stage1, "-src-repo", "workloads", "-dest-project", stage2, "-dest-repo", "promotions", "-fwder-project", "ret-edge-reg-fwder", "-src-location", east2, "-dest-location", napac, } expectedCfg := &Config{ LockFile: workloadLockFile, sourceProjectID: stage1, sourceRepoName: "workloads", SourceRepo: Repository{ Name: "workloads", ProjectID: stage1, Registry: east2 + dockerRegistrySuffix, Ref: path.Join(east2+dockerRegistrySuffix, stage1, "workloads"), }, destinationProjectID: stage2, destinationRepoName: "promotions", DestinationRepo: Repository{ Name: "promotions", ProjectID: stage2, Registry: napac + dockerRegistrySuffix, Ref: path.Join(napac+dockerRegistrySuffix, stage2, "promotions"), }, ForwarderProjectID: "ret-edge-reg-fwder", sourceLocation: east2, destinationLocation: napac, } cfg, err := newConfig() assert.NoError(t, err) assert.Equal(t, expectedCfg, cfg) os.Args = args } func TestNewConfigWithDefaults(t *testing.T) { args := os.Args os.Args = []string{ PromoteTag, "-lockfile", warehouseLockFile, "-src-project", stage1, "-dest-project", stage2, } expectedCfg := &Config{ LockFile: warehouseLockFile, sourceProjectID: stage1, sourceRepoName: WarehouseRepo, SourceRepo: Repository{ Name: WarehouseRepo, ProjectID: stage1, Registry: defaultGCPLocation + dockerRegistrySuffix, Ref: path.Join(defaultGCPLocation+dockerRegistrySuffix, stage1, WarehouseRepo), }, destinationProjectID: stage2, destinationRepoName: WarehouseRepo, DestinationRepo: Repository{ Name: WarehouseRepo, ProjectID: stage2, Registry: defaultGCPLocation + dockerRegistrySuffix, Ref: path.Join(defaultGCPLocation+dockerRegistrySuffix, stage2, WarehouseRepo), }, ForwarderProjectID: platformInfraProject, sourceLocation: defaultGCPLocation, destinationLocation: defaultGCPLocation, } cfg, err := newConfig() assert.NoError(t, err) assert.Equal(t, expectedCfg, cfg) os.Args = args } func TestNewConfigMissingRequiredFlagsReturnsError(t *testing.T) { args := os.Args os.Args = []string{ PromoteTag, "-src-project", stage1, // missing destination } cfg, err := newConfig() assert.Nil(t, cfg) assert.ErrorIs(t, err, errMissingConfig) os.Args = args } func TestNewConfigMissingAllFlagsReturnsError(t *testing.T) { args := os.Args os.Args = []string{PromoteTag} cfg, err := newConfig() assert.Nil(t, cfg) assert.ErrorIs(t, err, errMissingConfig) os.Args = args } func TestNewConfigWithUnknownFlagReturnsError(t *testing.T) { args := os.Args os.Args = []string{PromoteTag, "-invalid-flag", "value"} cfg, err := newConfig() assert.Nil(t, cfg) assert.Error(t, err) os.Args = args }