File: csm_test.go

package info (click to toggle)
golang-github-aws-aws-sdk-go 1.36.33-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, bullseye-backports
  • size: 163,900 kB
  • sloc: makefile: 182
file content (124 lines) | stat: -rw-r--r-- 2,512 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// +build go1.7

package session

import (
	"os"
	"path/filepath"
	"strings"
	"testing"

	"github.com/aws/aws-sdk-go/internal/sdktesting"
)

func TestSession_loadCSMConfig(t *testing.T) {
	defConfigFiles := []string{
		filepath.Join("testdata", "csm_shared_config"),
	}
	cases := map[string]struct {
		Envs        map[string]string
		ConfigFiles []string
		CSMProfile  string

		Expect csmConfig
		Err    string
	}{
		"no config": {
			Envs:        map[string]string{},
			Expect:      csmConfig{},
			ConfigFiles: defConfigFiles,
			CSMProfile:  "aws_csm_empty",
		},
		"env enabled": {
			Envs: map[string]string{
				"AWS_CSM_ENABLED":   "true",
				"AWS_CSM_PORT":      "4321",
				"AWS_CSM_HOST":      "ahost",
				"AWS_CSM_CLIENT_ID": "client id",
			},
			Expect: csmConfig{
				Enabled:  true,
				Port:     "4321",
				Host:     "ahost",
				ClientID: "client id",
			},
		},
		"shared cfg enabled": {
			ConfigFiles: defConfigFiles,
			Expect: csmConfig{
				Enabled:  true,
				Port:     "1234",
				Host:     "bar",
				ClientID: "foo",
			},
		},
		"mixed cfg, use env": {
			Envs: map[string]string{
				"AWS_CSM_ENABLED": "true",
			},
			ConfigFiles: defConfigFiles,
			Expect: csmConfig{
				Enabled: true,
			},
		},
		"mixed cfg, use env disabled": {
			Envs: map[string]string{
				"AWS_CSM_ENABLED": "false",
			},
			ConfigFiles: defConfigFiles,
			Expect: csmConfig{
				Enabled: false,
			},
		},
		"mixed cfg, use shared config": {
			Envs: map[string]string{
				"AWS_CSM_PORT": "4321",
			},
			ConfigFiles: defConfigFiles,
			Expect: csmConfig{
				Enabled:  true,
				Port:     "1234",
				Host:     "bar",
				ClientID: "foo",
			},
		},
	}

	for name, c := range cases {
		t.Run(name, func(t *testing.T) {
			restoreFn := sdktesting.StashEnv()
			defer restoreFn()

			if len(c.CSMProfile) != 0 {
				csmProfile := csmProfileName
				defer func() {
					csmProfileName = csmProfile
				}()
				csmProfileName = c.CSMProfile
			}

			for name, v := range c.Envs {
				os.Setenv(name, v)
			}

			envCfg, err := loadEnvConfig()
			if err != nil {
				t.Fatalf("failed to load the envcfg, %v", err)
			}
			csmCfg, err := loadCSMConfig(envCfg, c.ConfigFiles)
			if len(c.Err) != 0 {
				if err == nil {
					t.Fatalf("expect error, got none")
				}
				if e, a := c.Err, err.Error(); !strings.Contains(a, e) {
					t.Errorf("expect %v in error %v", e, a)
				}
				return
			}

			if e, a := c.Expect, csmCfg; e != a {
				t.Errorf("expect %v CSM config got %v", e, a)
			}
		})
	}
}