github.com/wunderio/silta-cli@v0.0.0-20240508100559-3017e4ab3a20/tests/image_test.go (about)

     1  package cmd_test
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  )
     7  
     8  func TestImageLoginCmd(t *testing.T) {
     9  
    10  	// Go to main directory
    11  	wd, _ := os.Getwd()
    12  	os.Chdir("..")
    13  
    14  	// Test env
    15  	command := "ci image login --debug"
    16  	environment := []string{
    17  		"IMAGE_REPO_HOST=foo.bar",
    18  		"GCLOUD_KEY_JSON=baz",
    19  	}
    20  	testString := `echo "baz" | docker login --username "_json_key" --password-stdin https://foo.bar`
    21  	CliExecTest(t, command, environment, testString, false)
    22  
    23  	// Test all env
    24  	command = "ci image login --debug"
    25  	environment = []string{
    26  		"IMAGE_REPO_HOST=foo.bar",
    27  		"IMAGE_REPO_USER=111",
    28  		"IMAGE_REPO_PASS=222",
    29  		"GCLOUD_KEY_JSON=333",
    30  		"AWS_SECRET_ACCESS_KEY=444",
    31  		"AWS_REGION=555",
    32  		"AKS_TENANT_ID=666",
    33  		"AKS_SP_APP_ID=777",
    34  		"AKS_SP_PASSWORD=888",
    35  	}
    36  	testString = `IMAGE_REPO_HOST: foo.bar
    37  IMAGE_REPO_TLS: true
    38  IMAGE_REPO_USER: 111
    39  IMAGE_REPO_PASS: 222
    40  GCLOUD_KEY_JSON: 333
    41  AWS_SECRET_ACCESS_KEY: 444
    42  AWS_REGION: 555
    43  AKS_TENANT_ID: 666
    44  AKS_SP_APP_ID: 777
    45  AKS_SP_PASSWORD: 888
    46  Command (not executed): echo "222" | docker login --username "111" --password-stdin https://foo.bar`
    47  
    48  	CliExecTest(t, command, environment, testString, false)
    49  
    50  	// Test undefined ENV
    51  	command = "ci image login --debug"
    52  	environment = []string{}
    53  	testString = `Docker registry credentials are empty`
    54  	CliExecTest(t, command, environment, testString, false)
    55  
    56  	// Test args
    57  	command = "ci image login --image-repo-host foo.bar --gcp-key-json baz --debug"
    58  	environment = []string{}
    59  	testString = `echo "baz" | docker login --username "_json_key" --password-stdin https://foo.bar`
    60  	CliExecTest(t, command, environment, testString, false)
    61  
    62  	// Test all args
    63  	command = `ci image login \
    64  			--image-repo-host foo.bar \
    65  			--image-repo-user 111 \
    66  			--image-repo-pass 222 \
    67  			--gcp-key-json 333 \
    68  			--aws-secret-access-key 444 \
    69  			--aws-region 555 \
    70  			--aks-tenant-id 666 \
    71  			--aks-sp-app-id 777 \
    72  			--aks-sp-password 888 \
    73  			--debug`
    74  
    75  	environment = []string{}
    76  	testString = `IMAGE_REPO_HOST: foo.bar
    77  IMAGE_REPO_TLS: true
    78  IMAGE_REPO_USER: 111
    79  IMAGE_REPO_PASS: 222
    80  GCLOUD_KEY_JSON: 333
    81  AWS_SECRET_ACCESS_KEY: 444
    82  AWS_REGION: 555
    83  AKS_TENANT_ID: 666
    84  AKS_SP_APP_ID: 777
    85  AKS_SP_PASSWORD: 888
    86  Command (not executed): echo "222" | docker login --username "111" --password-stdin https://foo.bar`
    87  	CliExecTest(t, command, environment, testString, false)
    88  
    89  	// Test args+env merge
    90  	command = "ci image login --image-repo-host foo.bar --debug"
    91  	environment = []string{
    92  		"IMAGE_REPO_HOST=bar.bar",
    93  		"GCLOUD_KEY_JSON=baz",
    94  	}
    95  	testString = `echo "baz" | docker login --username "_json_key" --password-stdin https://foo.bar`
    96  	CliExecTest(t, command, environment, testString, false)
    97  
    98  	// Change dir back to previous
    99  	os.Chdir(wd)
   100  }
   101  
   102  func TestImageUrlCmd(t *testing.T) {
   103  
   104  	// Go to main directory
   105  	wd, _ := os.Getwd()
   106  	os.Chdir("..")
   107  
   108  	// Incomplete flags test
   109  	command := "ci image url"
   110  	environment := []string{}
   111  	testString := `Error: required flag(s)`
   112  	CliExecTest(t, command, environment, testString, false)
   113  
   114  	// image-tag flag test
   115  	command = "ci image url --image-repo-host 'foo.bar' --image-repo-project 'silta' --namespace 'baz' --image-identifier 'nginx' --dockerfile 'tests/nginx.Dockerfile' --image-tag=qux"
   116  	environment = []string{}
   117  	testString = `foo.bar/silta/baz-nginx:qux`
   118  	CliExecTest(t, command, environment, testString, true)
   119  
   120  	// Change dir back to previous
   121  	os.Chdir(wd)
   122  }
   123  func TestImageBuildCmd(t *testing.T) {
   124  
   125  	// Go to main directory
   126  	wd, _ := os.Getwd()
   127  	os.Chdir("..")
   128  
   129  	// Incomplete flags test
   130  	command := "ci image build"
   131  	environment := []string{}
   132  	testString := `Error: required flag(s)`
   133  	CliExecTest(t, command, environment, testString, false)
   134  
   135  	// image-tag flag test
   136  	command = "ci image build --image-repo-host 'foo.bar' --image-repo-project 'silta' --namespace 'baz' --image-identifier 'nginx' --dockerfile 'tests/nginx.Dockerfile' --image-tag=qux --debug"
   137  	environment = []string{}
   138  	testString = `docker push 'foo.bar/silta/baz-nginx:qux'`
   139  	CliExecTest(t, command, environment, testString, false)
   140  
   141  	// Change dir back to previous
   142  	os.Chdir(wd)
   143  }