github.com/freiheit-com/kuberpult@v1.24.2-0.20240328135542-315d5630abe6/services/rollout-service/pkg/cmd/server_test.go (about) 1 /*This file is part of kuberpult. 2 3 Kuberpult is free software: you can redistribute it and/or modify 4 it under the terms of the Expat(MIT) License as published by 5 the Free Software Foundation. 6 7 Kuberpult is distributed in the hope that it will be useful, 8 but WITHOUT ANY WARRANTY; without even the implied warranty of 9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 MIT License for more details. 11 12 You should have received a copy of the MIT License 13 along with kuberpult. If not, see <https://directory.fsf.org/wiki/License:Expat>. 14 15 Copyright 2023 freiheit.com*/ 16 17 package cmd 18 19 import ( 20 "context" 21 "testing" 22 23 "github.com/google/go-cmp/cmp" 24 "github.com/google/go-cmp/cmp/cmpopts" 25 ) 26 27 // Used to compare two error message strings, needed because errors.Is(fmt.Errorf(text),fmt.Errorf(text)) == false 28 type errMatcher struct { 29 msg string 30 } 31 32 func (e errMatcher) Error() string { 33 return e.msg 34 } 35 36 func (e errMatcher) Is(err error) bool { 37 return e.Error() == err.Error() 38 } 39 40 func TestService(t *testing.T) { 41 tcs := []struct { 42 Name string 43 ExpectedError error 44 Config Config 45 }{ 46 { 47 Name: "simple case", 48 ExpectedError: errMatcher{"invalid argocd server url: parse \"\": empty url"}, 49 }, 50 { 51 Name: "invalid argocd url", 52 ExpectedError: errMatcher{"invalid argocd server url: parse \"not a http address\": invalid URI for request"}, 53 Config: Config{ 54 ArgocdServer: "not a http address", 55 }, 56 }, 57 { 58 Name: "valid http argocd url", 59 ExpectedError: errMatcher{"connecting to argocd version: dial tcp 127.0.0.1:32761: connect: connection refused"}, 60 Config: Config{ 61 ArgocdServer: "http://127.0.0.1:32761", 62 }, 63 }, 64 } 65 66 for _, tc := range tcs { 67 tc := tc 68 t.Run(tc.Name, func(t *testing.T) { 69 ctx := context.Background() 70 err := runServer(ctx, tc.Config) 71 if diff := cmp.Diff(tc.ExpectedError, err, cmpopts.EquateErrors()); diff != "" { 72 t.Errorf("error mismatch (-want, +got):\n%s", diff) 73 } 74 }) 75 } 76 } 77 78 func TestClientConfig(t *testing.T) { 79 tcs := []struct { 80 Name string 81 Config Config 82 83 ExpectedError error 84 ExpectedServerAddr string 85 ExpectedPlainText bool 86 }{ 87 { 88 Name: "simple plaintext", 89 Config: Config{ 90 ArgocdServer: "http://foo:80", 91 }, 92 ExpectedServerAddr: "foo:80", 93 ExpectedPlainText: true, 94 }, 95 { 96 Name: "simple tls", 97 Config: Config{ 98 ArgocdServer: "tls://foo:80", 99 }, 100 ExpectedServerAddr: "foo:80", 101 ExpectedPlainText: false, 102 }, 103 { 104 Name: "simple tls", 105 Config: Config{ 106 ArgocdServer: "not a url", 107 }, 108 ExpectedError: errMatcher{"invalid argocd server url: parse \"not a url\": invalid URI for request"}, 109 }, 110 } 111 for _, tc := range tcs { 112 tc := tc 113 t.Run(tc.Name, func(t *testing.T) { 114 clientConfig, err := tc.Config.ClientConfig() 115 if diff := cmp.Diff(tc.ExpectedError, err, cmpopts.EquateErrors()); diff != "" { 116 t.Errorf("error mismatch (-want, +got):\n%s", diff) 117 } 118 if clientConfig.ServerAddr != tc.ExpectedServerAddr { 119 t.Errorf("mismatched ServerAddr, expected %q, got %q", tc.ExpectedServerAddr, clientConfig.ServerAddr) 120 } 121 if clientConfig.PlainText != tc.ExpectedPlainText { 122 t.Errorf("mismatched PlainText, expected %t, got %t", tc.ExpectedPlainText, clientConfig.PlainText) 123 } 124 }) 125 } 126 }