github.com/kubeshop/testkube@v1.17.23/pkg/test/detector/detector_test.go (about) 1 package detector 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 8 "github.com/kubeshop/testkube/contrib/executor/artillery/pkg/artillery" 9 "github.com/kubeshop/testkube/contrib/executor/curl/pkg/curl" 10 "github.com/kubeshop/testkube/contrib/executor/jmeter/pkg/jmeter" 11 "github.com/kubeshop/testkube/contrib/executor/k6/pkg/k6detector" 12 "github.com/kubeshop/testkube/contrib/executor/postman/pkg/postman" 13 "github.com/kubeshop/testkube/contrib/executor/soapui/pkg/soapui" 14 "github.com/kubeshop/testkube/pkg/api/v1/client" 15 "github.com/kubeshop/testkube/pkg/api/v1/testkube" 16 ) 17 18 func TestDetectorDetect(t *testing.T) { 19 20 t.Run("Detect test returns success", func(t *testing.T) { 21 22 detector := Detector{Adapters: make(map[string]Adapter, 0)} 23 detector.Add(curl.Detector{}) 24 detector.Add(postman.Detector{}) 25 detector.Add(k6detector.Detector{}) 26 27 name, found := detector.Detect("postman_collection.json", client.UpsertTestOptions{ 28 Content: testkube.NewStringTestContent(examplePostmanContent), 29 }) 30 31 assert.True(t, found, "detector should find postman/collection") 32 assert.Equal(t, "postman/collection", name) 33 }) 34 35 } 36 37 func TestDetectorDetectTestName(t *testing.T) { 38 39 t.Run("Detect test name returns success", func(t *testing.T) { 40 41 detector := postman.Detector{} 42 43 name, found := detector.IsTestName("test.postman_collection.json") 44 45 assert.True(t, found, "detector should find postman/collection") 46 assert.Equal(t, "test", name) 47 }) 48 49 t.Run("Detect test name returns failure", func(t *testing.T) { 50 51 detector := postman.Detector{} 52 53 name, found := detector.IsTestName("test.json") 54 55 assert.False(t, found, "detector should not find test type") 56 assert.Empty(t, name) 57 }) 58 59 } 60 61 func TestDetectorDetectEnvName(t *testing.T) { 62 63 t.Run("Detect env name returns success", func(t *testing.T) { 64 65 detector := postman.Detector{} 66 67 name, envName, found := detector.IsEnvName("test.prod.postman_environment.json") 68 69 assert.True(t, found, "detector should find postman/collection") 70 assert.Equal(t, "test", name) 71 assert.Equal(t, "prod", envName) 72 }) 73 74 t.Run("Detect env name returns failure", func(t *testing.T) { 75 76 detector := postman.Detector{} 77 78 name, envName, found := detector.IsEnvName("test.prod.json") 79 80 assert.False(t, found, "detector should not find test type") 81 assert.Empty(t, name) 82 assert.Empty(t, envName) 83 }) 84 85 } 86 87 func TestDetectorDetectSecretEnvName(t *testing.T) { 88 89 t.Run("Detect secret env name returns success", func(t *testing.T) { 90 91 detector := postman.Detector{} 92 93 name, envName, found := detector.IsSecretEnvName("test.dev.postman_secret_environment.json") 94 95 assert.True(t, found, "detector should find postman/collection") 96 assert.Equal(t, "test", name) 97 assert.Equal(t, "dev", envName) 98 }) 99 100 t.Run("Detect secret env name returns failure", func(t *testing.T) { 101 102 detector := postman.Detector{} 103 104 name, envName, found := detector.IsSecretEnvName("test.dev.json") 105 106 assert.False(t, found, "detector should not find test type") 107 assert.Empty(t, name) 108 assert.Empty(t, envName) 109 }) 110 111 } 112 113 func TestDetectorGetAdapter(t *testing.T) { 114 115 t.Run("Get adapter returns success", func(t *testing.T) { 116 117 detector := Detector{Adapters: make(map[string]Adapter, 0)} 118 detector.Add(curl.Detector{}) 119 detector.Add(postman.Detector{}) 120 detector.Add(k6detector.Detector{}) 121 122 adapter := detector.GetAdapter("postman/collection") 123 124 assert.NotNil(t, adapter) 125 }) 126 127 } 128 129 func TestDifferentTestTypes(t *testing.T) { 130 t.Run("Detect postman collection", func(t *testing.T) { 131 detector := NewDefaultDetector() 132 name, found := detector.Detect("postman_collection.json", client.UpsertTestOptions{ 133 Content: testkube.NewStringTestContent(examplePostmanContent), 134 }) 135 136 assert.True(t, found, "detector should find postman/collection") 137 assert.Equal(t, postman.PostmanCollectionType, name) 138 }) 139 140 t.Run("Detect artillery test", func(t *testing.T) { 141 detector := NewDefaultDetector() 142 name, found := detector.Detect(exampleArtilleryFilename, client.UpsertTestOptions{ 143 Content: testkube.NewStringTestContent(exampleArtilleryContent), 144 }) 145 146 assert.True(t, found, "detector should find artillery/test") 147 assert.Equal(t, artillery.Type, name) 148 }) 149 150 t.Run("Detect cURL test", func(t *testing.T) { 151 detector := NewDefaultDetector() 152 name, found := detector.Detect(exampleCurlFilename, client.UpsertTestOptions{ 153 Content: testkube.NewStringTestContent(exampleCurlContent), 154 }) 155 156 assert.True(t, found, "detector should find curl/test") 157 assert.Equal(t, curl.Type, name) 158 }) 159 160 t.Run("Detect jmeter test", func(t *testing.T) { 161 detector := NewDefaultDetector() 162 name, found := detector.Detect(exampleJMeterFilename, client.UpsertTestOptions{ 163 Content: testkube.NewStringTestContent(exampleJMeterContent), 164 }) 165 166 assert.True(t, found, "detector should find jmeter/test") 167 assert.Equal(t, jmeter.Type, name) 168 }) 169 170 t.Run("Detect k6 test", func(t *testing.T) { 171 detector := NewDefaultDetector() 172 name, found := detector.Detect(exampleK6Filename, client.UpsertTestOptions{ 173 Content: testkube.NewStringTestContent(exampleK6Content), 174 }) 175 176 assert.True(t, found, "detector should find k6/test") 177 assert.Equal(t, k6detector.Type, name) 178 }) 179 180 t.Run("Detect soapui test", func(t *testing.T) { 181 detector := NewDefaultDetector() 182 name, found := detector.Detect(exampleSoapUIFilename, client.UpsertTestOptions{ 183 Content: testkube.NewStringTestContent(exampleSoapUIContent), 184 }) 185 186 assert.True(t, found, "detector should find soapui/test") 187 assert.Equal(t, soapui.Type, name) 188 }) 189 }