|
1 | 1 | package sdk
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "errors" |
| 5 | + "fmt" |
4 | 6 | "net/http"
|
5 | 7 | "net/http/httptest"
|
6 | 8 | "net/url"
|
7 | 9 | "testing"
|
| 10 | + |
| 11 | + "github.com/openfaas/faas-provider/types" |
8 | 12 | )
|
9 | 13 |
|
10 | 14 | func TestSdk_GetNamespaces_TwoNamespaces(t *testing.T) {
|
@@ -70,3 +74,132 @@ func TestSdk_GetNamespaces_NoNamespaces(t *testing.T) {
|
70 | 74 | t.Fatalf("want %d namespaces, got: %d", len(wantNS), gotNS)
|
71 | 75 | }
|
72 | 76 | }
|
| 77 | + |
| 78 | +func TestSdk_DeployFunction(t *testing.T) { |
| 79 | + funcName := "funct1" |
| 80 | + nsName := "ns1" |
| 81 | + tests := []struct { |
| 82 | + name string |
| 83 | + functionName string |
| 84 | + namespace string |
| 85 | + err error |
| 86 | + handler func(rw http.ResponseWriter, req *http.Request) |
| 87 | + }{ |
| 88 | + { |
| 89 | + name: "function deployed", |
| 90 | + functionName: funcName, |
| 91 | + namespace: nsName, |
| 92 | + handler: func(rw http.ResponseWriter, req *http.Request) { |
| 93 | + rw.WriteHeader(http.StatusOK) |
| 94 | + }, |
| 95 | + }, |
| 96 | + { |
| 97 | + name: "function will bedeployed", |
| 98 | + functionName: funcName, |
| 99 | + namespace: nsName, |
| 100 | + handler: func(rw http.ResponseWriter, req *http.Request) { |
| 101 | + rw.WriteHeader(http.StatusAccepted) |
| 102 | + }, |
| 103 | + }, |
| 104 | + { |
| 105 | + name: "client not authorized", |
| 106 | + functionName: funcName, |
| 107 | + namespace: nsName, |
| 108 | + handler: func(rw http.ResponseWriter, req *http.Request) { |
| 109 | + rw.WriteHeader(http.StatusUnauthorized) |
| 110 | + }, |
| 111 | + err: fmt.Errorf("unauthorized action, please setup authentication for this server"), |
| 112 | + }, |
| 113 | + { |
| 114 | + name: "unknown error", |
| 115 | + functionName: funcName, |
| 116 | + namespace: nsName, |
| 117 | + handler: func(rw http.ResponseWriter, req *http.Request) { |
| 118 | + http.Error(rw, "unknown error", http.StatusInternalServerError) |
| 119 | + }, |
| 120 | + err: fmt.Errorf("unexpected status code: %d, message: %q", http.StatusInternalServerError, "unknown error\n"), |
| 121 | + }, |
| 122 | + } |
| 123 | + |
| 124 | + for _, test := range tests { |
| 125 | + t.Run(test.name, func(t *testing.T) { |
| 126 | + s := httptest.NewServer(http.HandlerFunc(test.handler)) |
| 127 | + |
| 128 | + sU, _ := url.Parse(s.URL) |
| 129 | + |
| 130 | + client := NewClient(sU, nil, http.DefaultClient) |
| 131 | + _, err := client.Deploy(types.FunctionDeployment{ |
| 132 | + Service: funcName, |
| 133 | + Image: fmt.Sprintf("docker.io/openfaas/%s:latest", funcName), |
| 134 | + Namespace: nsName, |
| 135 | + }) |
| 136 | + |
| 137 | + if !errors.Is(err, test.err) && err.Error() != test.err.Error() { |
| 138 | + t.Fatalf("wanted %s, but got: %s", test.err, err) |
| 139 | + } |
| 140 | + }) |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +func TestSdk_DeleteFunction(t *testing.T) { |
| 145 | + funcName := "funct1" |
| 146 | + nsName := "ns1" |
| 147 | + tests := []struct { |
| 148 | + name string |
| 149 | + functionName string |
| 150 | + namespace string |
| 151 | + err error |
| 152 | + handler func(rw http.ResponseWriter, req *http.Request) |
| 153 | + }{ |
| 154 | + { |
| 155 | + name: "function deleted", |
| 156 | + functionName: funcName, |
| 157 | + namespace: nsName, |
| 158 | + handler: func(rw http.ResponseWriter, req *http.Request) { |
| 159 | + rw.WriteHeader(http.StatusAccepted) |
| 160 | + }, |
| 161 | + }, |
| 162 | + { |
| 163 | + name: "function not found", |
| 164 | + functionName: funcName, |
| 165 | + namespace: nsName, |
| 166 | + handler: func(rw http.ResponseWriter, req *http.Request) { |
| 167 | + rw.WriteHeader(http.StatusNotFound) |
| 168 | + }, |
| 169 | + err: fmt.Errorf("function %s not found", funcName), |
| 170 | + }, |
| 171 | + { |
| 172 | + name: "client not authorized", |
| 173 | + functionName: funcName, |
| 174 | + namespace: nsName, |
| 175 | + handler: func(rw http.ResponseWriter, req *http.Request) { |
| 176 | + rw.WriteHeader(http.StatusUnauthorized) |
| 177 | + }, |
| 178 | + err: fmt.Errorf("unauthorized action, please setup authentication for this server"), |
| 179 | + }, |
| 180 | + { |
| 181 | + name: "unknown error", |
| 182 | + functionName: funcName, |
| 183 | + namespace: nsName, |
| 184 | + handler: func(rw http.ResponseWriter, req *http.Request) { |
| 185 | + http.Error(rw, "unknown error", http.StatusInternalServerError) |
| 186 | + }, |
| 187 | + err: fmt.Errorf("server returned unexpected status code %d, message: %q", http.StatusInternalServerError, string("unknown error\n")), |
| 188 | + }, |
| 189 | + } |
| 190 | + |
| 191 | + for _, test := range tests { |
| 192 | + t.Run(test.name, func(t *testing.T) { |
| 193 | + s := httptest.NewServer(http.HandlerFunc(test.handler)) |
| 194 | + |
| 195 | + sU, _ := url.Parse(s.URL) |
| 196 | + |
| 197 | + client := NewClient(sU, nil, http.DefaultClient) |
| 198 | + err := client.DeleteFunction(test.functionName, test.namespace) |
| 199 | + |
| 200 | + if !errors.Is(err, test.err) && err.Error() != test.err.Error() { |
| 201 | + t.Fatalf("wanted %s, but got: %s", test.err, err) |
| 202 | + } |
| 203 | + }) |
| 204 | + } |
| 205 | +} |
0 commit comments