@@ -4,35 +4,89 @@ import (
4
4
"fmt"
5
5
"testing"
6
6
7
- "github.com/hashicorp/go-version"
8
7
"github.com/stretchr/testify/assert"
9
8
)
10
9
11
- var helmVerTpl = "version.BuildInfo{Version:\" %s\" , GitCommit:\" 50f003e5ee8704ec937a756c646870227d7c8b58\" , GitTreeState:\" clean\" , GoVersion:\" go1.18.8\" }"
10
+ var outputTemplates = map [string ]string {
11
+ "aws" : "aws-cli/%s Python/3.11.5 Linux/5.15.90.1 source/x86_64.ubuntu.22 prompt/off" ,
12
+ "az" : `{
13
+ "azure-cli": "%s",
14
+ "azure-cli-core": "2.52.0",
15
+ "azure-cli-telemetry": "1.1.0",
16
+ "extensions": {}
17
+ }` ,
18
+ "gcloud" : `Google Cloud SDK %s
19
+ alpha 2023.09.13
20
+ beta 2023.09.13
21
+ bq 2.0.98
22
+ bundled-python3-unix 3.9.16
23
+ core 2023.09.13
24
+ gcloud-crc32c 1.0.0
25
+ gsutil 5.25` ,
26
+ "gsutil" : "version: %s" ,
27
+ "vault" : "Vault v%s ('56debfa71653e72433345f23cd26276bc90629ce+CHANGES'), built 2023-09-11T21:23:55Z" ,
28
+ "kubectl" : `{
29
+ "clientVersion": {
30
+ "major": "1",
31
+ "minor": "28",
32
+ "gitVersion": "v%s",
33
+ "gitCommit": "8dc49c4b984b897d423aab4971090e1879eb4f23",
34
+ "gitTreeState": "clean",
35
+ "buildDate": "2023-08-24T11:16:29Z",
36
+ "goVersion": "go1.20.7",
37
+ "compiler": "gc",
38
+ "platform": "linux/amd64"
39
+ },
40
+ "kustomizeVersion": "v5.0.4-0.20230601165947-6ce0bf390ce3"
41
+ }` ,
42
+ "helm" : "version.BuildInfo{Version:\" v%s\" , GitCommit:\" 3a31588ad33fe3b89af5a2a54ee1d25bfe6eaa5e\" , GitTreeState:\" clean\" , GoVersion:\" go1.20.7\" }" ,
43
+ "terraform" : "Terraform v%s\n on linux_amd64" ,
44
+ }
45
+
46
+ func formatVersion (binary , version string ) []byte {
47
+ return []byte (fmt .Sprintf (outputTemplates [binary ], version ))
48
+ }
49
+
50
+ func testRequiredbinaryVersion (binary , equalVersion , newerVersion , olderVersion , startFromTen string , t * testing.T ) {
51
+ reqVer := binVersion [binary ]
52
+ err := checkRequiresBinVersion (reqVer , formatVersion (binary , equalVersion ))
53
+ assert .NoError (t , err , "When version is equal with minimal required, checkRequiresBinVersion should not return validation error" )
54
+ err = checkRequiresBinVersion (reqVer , formatVersion (binary , newerVersion ))
55
+ assert .NoError (t , err , "When version is greater than minimal required, checkRequiresBinVersion should not return validation error" )
56
+ err = checkRequiresBinVersion (reqVer , formatVersion (binary , olderVersion ))
57
+ assert .Error (t , err , "When version is less than minimal required, checkRequiresBinVersion should return validation error" )
58
+ err = checkRequiresBinVersion (reqVer , formatVersion (binary , startFromTen ))
59
+ assert .NoError (t , err , "When version number starts with 1 but actually is 10, checkRequiresBinVersion should not return validation error" )
60
+ }
61
+
62
+ func TestCheckAwsVersion (t * testing.T ) {
63
+ testRequiredbinaryVersion ("aws" , "2.10" , "2.13.19" , "2.0.2" , "10.1.1" , t )
64
+ }
12
65
13
- func formatHelmVersion ( version string ) [] byte {
14
- return [] byte ( fmt . Sprintf ( helmVerTpl , version ) )
66
+ func TestCheckAzureVersion ( t * testing. T ) {
67
+ testRequiredbinaryVersion ( "az" , "2.40" , "2.52.0" , "2.0.2" , "10.1.1" , t )
15
68
}
16
69
17
- func TestCheckRequiresBinVersion (t * testing.T ) {
18
- min , _ := version .NewVersion ("3.5.2" )
19
- helm := binVersion ["helm" ]
20
- helm .minVersion = min
70
+ func TestCheckGcloudVersion (t * testing.T ) {
71
+ testRequiredbinaryVersion ("gcloud" , "400.0.0" , "446.0.1" , "140.0.2" , "1010.1.1" , t )
72
+ }
21
73
22
- raw_data := formatHelmVersion ( min . String ())
23
- err := checkRequiresBinVersion ( helm , raw_data )
24
- assert . Error ( t , err , "When versions are equal, checkRequiresBinVersion should not return validation error" )
74
+ func TestCheckGsutilVersion ( t * testing. T ) {
75
+ testRequiredbinaryVersion ( "gsutil" , "5.0" , "5.25" , "3.52" , "10.1.1" , t )
76
+ }
25
77
26
- raw_data = formatHelmVersion ( "v0.0.1" )
27
- err = checkRequiresBinVersion ( helm , raw_data )
28
- assert . Error ( t , err , "When version is less than required, checkRequiresBinVersion should return validation error" )
78
+ func TestCheckVaultVersion ( t * testing. T ) {
79
+ testRequiredbinaryVersion ( "vault" , "1.10.0" , "1.14.3" , "1.9.10" , "10.1.1" , t )
80
+ }
29
81
30
- raw_data = formatHelmVersion ( "v100.0.0" )
31
- err = checkRequiresBinVersion ( helm , raw_data )
32
- assert . NoError ( t , err , "When version is greater than required, checkRequiresBinVersion should not return validation error" )
82
+ func TestCheckKubectlVersion ( t * testing. T ) {
83
+ testRequiredbinaryVersion ( "kubectl" , "1.19" , "1.28.1" , "1.18.15" , "10.1.1" , t )
84
+ }
33
85
34
- raw_data = formatHelmVersion ( "v3.10.2" )
35
- err = checkRequiresBinVersion ( helm , raw_data )
36
- assert . NoError ( t , err , "When version number starts with 1 but actually is 10 there should be no error" )
86
+ func TestCheckHelmVersion ( t * testing. T ) {
87
+ testRequiredbinaryVersion ( " helm" , "3.11" , "3.12.3" , "3.5.1" , "10.1.1" , t )
88
+ }
37
89
90
+ func TestCheckTerraformVersion (t * testing.T ) {
91
+ testRequiredbinaryVersion ("terraform" , "1.0" , "1.5.7" , "0.14.1" , "10.1.1" , t )
38
92
}
0 commit comments