...

Source file src/edge-infra.dev/pkg/f8n/warehouse/promote/config_test.go

Documentation: edge-infra.dev/pkg/f8n/warehouse/promote

     1  package promote
     2  
     3  import (
     4  	"os"
     5  	"path"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  var warehouseLockFile = "testdata/package_lock/warehouse-lock-pass.yaml"
    12  var workloadLockFile = "testdata/package_lock/workload-lock-pass.yaml"
    13  
    14  var (
    15  	stage1 = "ret-edge-stage1-foreman"
    16  	stage2 = "ret-edge-stage2-foreman"
    17  )
    18  
    19  func TestNewConfigForPackagePromotion(t *testing.T) {
    20  	east2 := "us-east2"
    21  	const napac = "napac"
    22  
    23  	args := os.Args
    24  	os.Args = []string{
    25  		PromoteTag,
    26  		"-lockfile", workloadLockFile,
    27  		"-src-project", stage1,
    28  		"-src-repo", "workloads",
    29  		"-dest-project", stage2,
    30  		"-dest-repo", "promotions",
    31  		"-fwder-project", "ret-edge-reg-fwder",
    32  		"-src-location", east2,
    33  		"-dest-location", napac,
    34  	}
    35  
    36  	expectedCfg := &Config{
    37  		LockFile:        workloadLockFile,
    38  		sourceProjectID: stage1,
    39  		sourceRepoName:  "workloads",
    40  		SourceRepo: Repository{
    41  			Name:      "workloads",
    42  			ProjectID: stage1,
    43  			Registry:  east2 + dockerRegistrySuffix,
    44  			Ref:       path.Join(east2+dockerRegistrySuffix, stage1, "workloads"),
    45  		},
    46  		destinationProjectID: stage2,
    47  		destinationRepoName:  "promotions",
    48  		DestinationRepo: Repository{
    49  			Name:      "promotions",
    50  			ProjectID: stage2,
    51  			Registry:  napac + dockerRegistrySuffix,
    52  			Ref:       path.Join(napac+dockerRegistrySuffix, stage2, "promotions"),
    53  		},
    54  		ForwarderProjectID:  "ret-edge-reg-fwder",
    55  		sourceLocation:      east2,
    56  		destinationLocation: napac,
    57  	}
    58  
    59  	cfg, err := newConfig()
    60  	assert.NoError(t, err)
    61  	assert.Equal(t, expectedCfg, cfg)
    62  
    63  	os.Args = args
    64  }
    65  
    66  func TestNewConfigWithDefaults(t *testing.T) {
    67  	args := os.Args
    68  	os.Args = []string{
    69  		PromoteTag,
    70  		"-lockfile", warehouseLockFile,
    71  		"-src-project", stage1,
    72  		"-dest-project", stage2,
    73  	}
    74  
    75  	expectedCfg := &Config{
    76  		LockFile:        warehouseLockFile,
    77  		sourceProjectID: stage1,
    78  		sourceRepoName:  WarehouseRepo,
    79  		SourceRepo: Repository{
    80  			Name:      WarehouseRepo,
    81  			ProjectID: stage1,
    82  			Registry:  defaultGCPLocation + dockerRegistrySuffix,
    83  			Ref:       path.Join(defaultGCPLocation+dockerRegistrySuffix, stage1, WarehouseRepo),
    84  		},
    85  		destinationProjectID: stage2,
    86  		destinationRepoName:  WarehouseRepo,
    87  		DestinationRepo: Repository{
    88  			Name:      WarehouseRepo,
    89  			ProjectID: stage2,
    90  			Registry:  defaultGCPLocation + dockerRegistrySuffix,
    91  			Ref:       path.Join(defaultGCPLocation+dockerRegistrySuffix, stage2, WarehouseRepo),
    92  		},
    93  		ForwarderProjectID:  platformInfraProject,
    94  		sourceLocation:      defaultGCPLocation,
    95  		destinationLocation: defaultGCPLocation,
    96  	}
    97  
    98  	cfg, err := newConfig()
    99  	assert.NoError(t, err)
   100  	assert.Equal(t, expectedCfg, cfg)
   101  
   102  	os.Args = args
   103  }
   104  
   105  func TestNewConfigMissingRequiredFlagsReturnsError(t *testing.T) {
   106  	args := os.Args
   107  	os.Args = []string{
   108  		PromoteTag,
   109  		"-src-project", stage1,
   110  		// missing destination
   111  	}
   112  
   113  	cfg, err := newConfig()
   114  	assert.Nil(t, cfg)
   115  	assert.ErrorIs(t, err, errMissingConfig)
   116  
   117  	os.Args = args
   118  }
   119  
   120  func TestNewConfigMissingAllFlagsReturnsError(t *testing.T) {
   121  	args := os.Args
   122  	os.Args = []string{PromoteTag}
   123  
   124  	cfg, err := newConfig()
   125  	assert.Nil(t, cfg)
   126  	assert.ErrorIs(t, err, errMissingConfig)
   127  
   128  	os.Args = args
   129  }
   130  
   131  func TestNewConfigWithUnknownFlagReturnsError(t *testing.T) {
   132  	args := os.Args
   133  	os.Args = []string{PromoteTag, "-invalid-flag", "value"}
   134  
   135  	cfg, err := newConfig()
   136  	assert.Nil(t, cfg)
   137  	assert.Error(t, err)
   138  
   139  	os.Args = args
   140  }
   141  

View as plain text