|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/spf13/cobra" |
| 9 | + "github.com/spf13/viper" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +func TestAllFlags(t *testing.T) { |
| 14 | + testCases := []struct { |
| 15 | + name string |
| 16 | + env map[string]string |
| 17 | + cli []string |
| 18 | + want globalConfig |
| 19 | + }{ |
| 20 | + { |
| 21 | + name: "defaults only", |
| 22 | + env: nil, |
| 23 | + cli: nil, |
| 24 | + want: globalConfig{ |
| 25 | + endpoints: []string{"127.0.0.1:2379"}, |
| 26 | + useClusterEndpoints: false, |
| 27 | + excludeLocalhost: false, |
| 28 | + moveLeader: false, |
| 29 | + dialTimeout: 2 * time.Second, |
| 30 | + commandTimeout: 30 * time.Second, |
| 31 | + keepAliveTime: 2 * time.Second, |
| 32 | + keepAliveTimeout: 6 * time.Second, |
| 33 | + insecure: true, |
| 34 | + insecureSkepVerify: false, |
| 35 | + certFile: "", |
| 36 | + keyFile: "", |
| 37 | + caFile: "", |
| 38 | + username: "", |
| 39 | + password: "", |
| 40 | + dnsDomain: "", |
| 41 | + dnsService: "", |
| 42 | + insecureDiscovery: true, |
| 43 | + compaction: true, |
| 44 | + continueOnError: true, |
| 45 | + dbQuotaBytes: 2 * 1024 * 1024 * 1024, |
| 46 | + defragRule: "", |
| 47 | + printVersion: false, |
| 48 | + dryRun: false, |
| 49 | + }, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "all from environment", |
| 53 | + env: map[string]string{ |
| 54 | + "ETCD_DEFRAG_ENDPOINTS": "10.0.0.1:2379,10.0.0.2:2379", |
| 55 | + "ETCD_DEFRAG_CLUSTER": "true", |
| 56 | + "ETCD_DEFRAG_EXCLUDE_LOCALHOST": "true", |
| 57 | + "ETCD_DEFRAG_MOVE_LEADER": "true", |
| 58 | + "ETCD_DEFRAG_DIAL_TIMEOUT": "5s", |
| 59 | + "ETCD_DEFRAG_COMMAND_TIMEOUT": "45s", |
| 60 | + "ETCD_DEFRAG_KEEPALIVE_TIME": "3s", |
| 61 | + "ETCD_DEFRAG_KEEPALIVE_TIMEOUT": "8s", |
| 62 | + "ETCD_DEFRAG_INSECURE_TRANSPORT": "false", |
| 63 | + "ETCD_DEFRAG_INSECURE_SKIP_TLS_VERIFY": "true", |
| 64 | + "ETCD_DEFRAG_CERT": "/path/to/cert", |
| 65 | + "ETCD_DEFRAG_KEY": "/path/to/key", |
| 66 | + "ETCD_DEFRAG_CACERT": "/path/to/ca", |
| 67 | + "ETCD_DEFRAG_USER": "envuser", |
| 68 | + "ETCD_DEFRAG_PASSWORD": "envpassword", |
| 69 | + "ETCD_DEFRAG_DISCOVERY_SRV": "mydomain.com", |
| 70 | + "ETCD_DEFRAG_DISCOVERY_SRV_NAME": "etcd", |
| 71 | + "ETCD_DEFRAG_INSECURE_DISCOVERY": "false", |
| 72 | + "ETCD_DEFRAG_COMPACTION": "false", |
| 73 | + "ETCD_DEFRAG_CONTINUE_ON_ERROR": "false", |
| 74 | + "ETCD_DEFRAG_ETCD_STORAGE_QUOTA_BYTES": "1073741824", |
| 75 | + "ETCD_DEFRAG_DEFRAG_RULE": "size(db) > 500MB", |
| 76 | + "ETCD_DEFRAG_VERSION": "true", |
| 77 | + "ETCD_DEFRAG_DRY_RUN": "true", |
| 78 | + }, |
| 79 | + cli: nil, |
| 80 | + want: globalConfig{ |
| 81 | + endpoints: []string{"10.0.0.1:2379", "10.0.0.2:2379"}, |
| 82 | + useClusterEndpoints: true, |
| 83 | + excludeLocalhost: true, |
| 84 | + moveLeader: true, |
| 85 | + dialTimeout: 5 * time.Second, |
| 86 | + commandTimeout: 45 * time.Second, |
| 87 | + keepAliveTime: 3 * time.Second, |
| 88 | + keepAliveTimeout: 8 * time.Second, |
| 89 | + insecure: false, |
| 90 | + insecureSkepVerify: true, |
| 91 | + certFile: "/path/to/cert", |
| 92 | + keyFile: "/path/to/key", |
| 93 | + caFile: "/path/to/ca", |
| 94 | + username: "envuser", |
| 95 | + password: "envpassword", |
| 96 | + dnsDomain: "mydomain.com", |
| 97 | + dnsService: "etcd", |
| 98 | + insecureDiscovery: false, |
| 99 | + compaction: false, |
| 100 | + continueOnError: false, |
| 101 | + dbQuotaBytes: 1073741824, |
| 102 | + defragRule: "size(db) > 500MB", |
| 103 | + printVersion: true, |
| 104 | + dryRun: true, |
| 105 | + }, |
| 106 | + }, |
| 107 | + { |
| 108 | + name: "all from CLI (override environment)", |
| 109 | + env: map[string]string{ |
| 110 | + "ETCD_DEFRAG_ENDPOINTS": "shouldBeOverridden:9999", |
| 111 | + "ETCD_DEFRAG_CLUSTER": "false", |
| 112 | + "ETCD_DEFRAG_MOVE_LEADER": "false", |
| 113 | + "ETCD_DEFRAG_INSECURE_TRANSPORT": "true", |
| 114 | + }, |
| 115 | + cli: []string{ |
| 116 | + "--endpoints=192.168.1.100:2379,192.168.1.101:2379", |
| 117 | + "--cluster=true", |
| 118 | + "--exclude-localhost=true", |
| 119 | + "--move-leader=true", |
| 120 | + "--dial-timeout=7s", |
| 121 | + "--command-timeout=50s", |
| 122 | + "--keepalive-time=4s", |
| 123 | + "--keepalive-timeout=10s", |
| 124 | + "--insecure-transport=false", |
| 125 | + "--insecure-skip-tls-verify=true", |
| 126 | + "--cert=/cli/cert", |
| 127 | + "--key=/cli/key", |
| 128 | + "--cacert=/cli/ca", |
| 129 | + "--user=cliuser", |
| 130 | + "--password=clipass", |
| 131 | + "--discovery-srv=cli.mydomain", |
| 132 | + "--discovery-srv-name=clietcd", |
| 133 | + "--insecure-discovery=false", |
| 134 | + "--compaction=false", |
| 135 | + "--continue-on-error=false", |
| 136 | + "--etcd-storage-quota-bytes=999999999", |
| 137 | + "--defrag-rule=size(db) >= 1GB", |
| 138 | + "--version=true", |
| 139 | + "--dry-run=true", |
| 140 | + }, |
| 141 | + want: globalConfig{ |
| 142 | + endpoints: []string{"192.168.1.100:2379", "192.168.1.101:2379"}, |
| 143 | + useClusterEndpoints: true, |
| 144 | + excludeLocalhost: true, |
| 145 | + moveLeader: true, |
| 146 | + dialTimeout: 7 * time.Second, |
| 147 | + commandTimeout: 50 * time.Second, |
| 148 | + keepAliveTime: 4 * time.Second, |
| 149 | + keepAliveTimeout: 10 * time.Second, |
| 150 | + insecure: false, |
| 151 | + insecureSkepVerify: true, |
| 152 | + certFile: "/cli/cert", |
| 153 | + keyFile: "/cli/key", |
| 154 | + caFile: "/cli/ca", |
| 155 | + username: "cliuser", |
| 156 | + password: "clipass", |
| 157 | + dnsDomain: "cli.mydomain", |
| 158 | + dnsService: "clietcd", |
| 159 | + insecureDiscovery: false, |
| 160 | + compaction: false, |
| 161 | + continueOnError: false, |
| 162 | + dbQuotaBytes: 999999999, |
| 163 | + defragRule: "size(db) >= 1GB", |
| 164 | + printVersion: true, |
| 165 | + dryRun: true, |
| 166 | + }, |
| 167 | + }, |
| 168 | + { |
| 169 | + name: "mixed env + CLI", |
| 170 | + env: map[string]string{ |
| 171 | + "ETCD_DEFRAG_ENDPOINTS": "env:2379", |
| 172 | + "ETCD_DEFRAG_CLUSTER": "false", |
| 173 | + "ETCD_DEFRAG_MOVE_LEADER": "true", |
| 174 | + "ETCD_DEFRAG_COMPACTION": "false", |
| 175 | + "ETCD_DEFRAG_ETCD_STORAGE_QUOTA_BYTES": "555555555", |
| 176 | + }, |
| 177 | + cli: []string{ |
| 178 | + "--exclude-localhost=true", // override the default |
| 179 | + "--dial-timeout=10s", // override the default |
| 180 | + "--compaction=true", // override the env |
| 181 | + }, |
| 182 | + want: globalConfig{ |
| 183 | + endpoints: []string{"env:2379"}, |
| 184 | + useClusterEndpoints: false, // env sets cluster=false |
| 185 | + excludeLocalhost: true, // from CLI |
| 186 | + moveLeader: true, // from env |
| 187 | + dialTimeout: 10 * time.Second, |
| 188 | + commandTimeout: 30 * time.Second, // default |
| 189 | + keepAliveTime: 2 * time.Second, // default |
| 190 | + keepAliveTimeout: 6 * time.Second, // default |
| 191 | + insecure: true, // default |
| 192 | + insecureSkepVerify: false, // default |
| 193 | + certFile: "", |
| 194 | + keyFile: "", |
| 195 | + caFile: "", |
| 196 | + username: "", |
| 197 | + password: "", |
| 198 | + dnsDomain: "", |
| 199 | + dnsService: "", |
| 200 | + insecureDiscovery: true, |
| 201 | + compaction: true, // CLI override |
| 202 | + continueOnError: true, // default |
| 203 | + dbQuotaBytes: 555555555, // from env |
| 204 | + defragRule: "", |
| 205 | + printVersion: false, // default |
| 206 | + dryRun: false, // default |
| 207 | + }, |
| 208 | + }, |
| 209 | + } |
| 210 | + |
| 211 | + for _, tc := range testCases { |
| 212 | + t.Run(tc.name, func(t *testing.T) { |
| 213 | + viper.Reset() |
| 214 | + os.Clearenv() |
| 215 | + |
| 216 | + for key, val := range tc.env { |
| 217 | + if err := os.Setenv(key, val); err != nil { |
| 218 | + t.Fatalf("failed to set env %s=%s: %v", key, val, err) |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + cmd := newDefragCommand() |
| 223 | + // Set empty Run function to avoid actual execution |
| 224 | + cmd.Run = func(cmd *cobra.Command, args []string) {} |
| 225 | + |
| 226 | + if tc.cli != nil { |
| 227 | + cmd.SetArgs(tc.cli) |
| 228 | + } |
| 229 | + |
| 230 | + if err := cmd.Execute(); err != nil { |
| 231 | + t.Fatalf("command execution failed: %v", err) |
| 232 | + } |
| 233 | + |
| 234 | + require.Equal(t, tc.want, globalCfg) |
| 235 | + }) |
| 236 | + } |
| 237 | +} |
0 commit comments