github.com/kyma-project/kyma/components/asset-store-controller-manager@v0.0.0-20191203152857-3792b5df17c5/internal/assethook/processor_test.go (about) 1 package assethook_test 2 3 import ( 4 "context" 5 "fmt" 6 "io" 7 "io/ioutil" 8 "net/http" 9 "strings" 10 "testing" 11 12 "github.com/kyma-project/kyma/components/asset-store-controller-manager/internal/assethook" 13 "github.com/kyma-project/kyma/components/asset-store-controller-manager/internal/assethook/automock" 14 "github.com/kyma-project/kyma/components/asset-store-controller-manager/pkg/apis/assetstore/v1alpha2" 15 "github.com/onsi/gomega" 16 "github.com/stretchr/testify/mock" 17 ) 18 19 func TestProcessor_Do(t *testing.T) { 20 t.Run("Success", func(t *testing.T) { 21 // Given 22 g := gomega.NewGomegaWithT(t) 23 24 service := fixService("test", "test", "/test") 25 client := new(automock.HttpClient) 26 defer client.AssertExpectations(t) 27 client.On("Do", mock.Anything).Return(fixHttpResponse(http.StatusOK, ""), nil).Once() 28 29 processor := assethook.NewProcessor(2, client, false, testCallback(nil, nil), testCallback(nil, nil)) 30 31 // When 32 result, err := processor.Do(context.TODO(), "./", []string{"processor_test.go"}, []v1alpha2.AssetWebhookService{service}) 33 34 // Then 35 g.Expect(err).ToNot(gomega.HaveOccurred()) 36 g.Expect(result).To(gomega.HaveLen(0)) 37 }) 38 39 t.Run("Error on service call", func(t *testing.T) { 40 // Given 41 g := gomega.NewGomegaWithT(t) 42 43 service := fixService("test", "test", "/test") 44 client := new(automock.HttpClient) 45 defer client.AssertExpectations(t) 46 client.On("Do", mock.Anything).Return(fixHttpResponse(http.StatusInternalServerError, ""), nil).Once() 47 48 processor := assethook.NewProcessor(2, client, false, testCallback(nil, nil), testCallback(nil, nil)) 49 50 // When 51 _, err := processor.Do(context.TODO(), "./", []string{"processor_test.go"}, []v1alpha2.AssetWebhookService{service}) 52 53 // Then 54 g.Expect(err).To(gomega.HaveOccurred()) 55 }) 56 57 t.Run("Message on failed callback", func(t *testing.T) { 58 // Given 59 g := gomega.NewGomegaWithT(t) 60 61 service := fixService("test", "test", "/test") 62 client := new(automock.HttpClient) 63 defer client.AssertExpectations(t) 64 client.On("Do", mock.Anything).Return(fixHttpResponse(http.StatusUnprocessableEntity, "message"), nil).Once() 65 66 processor := assethook.NewProcessor(2, client, false, testCallback(nil, nil), testCallback([]string{"err"}, nil)) 67 68 // When 69 result, err := processor.Do(context.TODO(), "./", []string{"processor_test.go"}, []v1alpha2.AssetWebhookService{service}) 70 71 // Then 72 g.Expect(err).ToNot(gomega.HaveOccurred()) 73 g.Expect(result).To(gomega.HaveLen(1)) 74 }) 75 76 t.Run("Error on failed callback", func(t *testing.T) { 77 // Given 78 g := gomega.NewGomegaWithT(t) 79 80 service := fixService("test", "test", "/test") 81 client := new(automock.HttpClient) 82 defer client.AssertExpectations(t) 83 client.On("Do", mock.Anything).Return(fixHttpResponse(http.StatusUnprocessableEntity, "message"), nil).Once() 84 85 processor := assethook.NewProcessor(2, client, false, testCallback(nil, nil), testCallback(nil, []error{fmt.Errorf("test")})) 86 87 // When 88 result, err := processor.Do(context.TODO(), "./", []string{"processor_test.go"}, []v1alpha2.AssetWebhookService{service}) 89 90 // Then 91 g.Expect(err).To(gomega.HaveOccurred()) 92 g.Expect(result).To(gomega.HaveLen(0)) 93 }) 94 95 t.Run("Message on success callback", func(t *testing.T) { 96 // Given 97 g := gomega.NewGomegaWithT(t) 98 99 service := fixService("test", "test", "/test") 100 client := new(automock.HttpClient) 101 defer client.AssertExpectations(t) 102 client.On("Do", mock.Anything).Return(fixHttpResponse(http.StatusOK, "message"), nil).Once() 103 104 processor := assethook.NewProcessor(2, client, false, testCallback([]string{"err"}, nil), testCallback(nil, nil)) 105 106 // When 107 result, err := processor.Do(context.TODO(), "./", []string{"processor_test.go"}, []v1alpha2.AssetWebhookService{service}) 108 109 // Then 110 g.Expect(err).ToNot(gomega.HaveOccurred()) 111 g.Expect(result).To(gomega.HaveLen(1)) 112 }) 113 114 t.Run("Error on success callback", func(t *testing.T) { 115 // Given 116 g := gomega.NewGomegaWithT(t) 117 118 service := fixService("test", "test", "/test") 119 client := new(automock.HttpClient) 120 defer client.AssertExpectations(t) 121 client.On("Do", mock.Anything).Return(fixHttpResponse(http.StatusOK, "message"), nil).Once() 122 123 processor := assethook.NewProcessor(2, client, false, testCallback(nil, []error{fmt.Errorf("test")}), testCallback(nil, nil)) 124 125 // When 126 result, err := processor.Do(context.TODO(), "./", []string{"processor_test.go"}, []v1alpha2.AssetWebhookService{service}) 127 128 // Then 129 g.Expect(err).To(gomega.HaveOccurred()) 130 g.Expect(result).To(gomega.HaveLen(0)) 131 }) 132 133 t.Run("Invalid file", func(t *testing.T) { 134 // Given 135 g := gomega.NewGomegaWithT(t) 136 137 service := fixService("test", "test", "/test") 138 client := new(automock.HttpClient) 139 defer client.AssertExpectations(t) 140 141 processor := assethook.NewProcessor(2, client, false, testCallback(nil, nil), testCallback(nil, nil)) 142 143 // When 144 result, err := processor.Do(context.TODO(), "./", []string{"xyz.go"}, []v1alpha2.AssetWebhookService{service}) 145 146 // Then 147 g.Expect(err).To(gomega.HaveOccurred()) 148 g.Expect(result).To(gomega.HaveLen(0)) 149 }) 150 } 151 152 func testCallback(messages []string, errors []error) assethook.Callback { 153 return func(ctx context.Context, basePath, filePath string, responseBody io.Reader, messagesChan chan assethook.Message, errChan chan error) { 154 for _, err := range errors { 155 errChan <- err 156 } 157 158 for _, msg := range messages { 159 messagesChan <- assethook.Message{Filename: filePath, Message: msg} 160 } 161 } 162 } 163 164 func fixService(name, namespace, endpoint string) v1alpha2.AssetWebhookService { 165 return v1alpha2.AssetWebhookService{ 166 WebhookService: v1alpha2.WebhookService{ 167 Name: name, 168 Namespace: namespace, 169 Endpoint: endpoint, 170 }, 171 } 172 } 173 174 func fixHttpResponse(statusCode int, body string) *http.Response { 175 return &http.Response{ 176 StatusCode: statusCode, 177 Body: ioutil.NopCloser(strings.NewReader(body)), 178 } 179 }