sigs.k8s.io/external-dns@v0.14.1/provider/bluecat/gateway/api_test.go (about)

     1  /*
     2  Copyright 2020 The Kubernetes Authors.
     3  Licensed under the Apache License, Version 2.0 (the "License");
     4  you may not use this file except in compliance with the License.
     5  You may obtain a copy of the License at
     6      http://www.apache.org/licenses/LICENSE-2.0
     7  Unless required by applicable law or agreed to in writing, software
     8  distributed under the License is distributed on an "AS IS" BASIS,
     9  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10  See the License for the specific language governing permissions and
    11  limitations under the License.
    12  */
    13  
    14  package api
    15  
    16  import (
    17  	"encoding/json"
    18  	"io"
    19  	"net/http"
    20  	"net/http/httptest"
    21  	"strings"
    22  	"testing"
    23  
    24  	"github.com/google/go-cmp/cmp"
    25  )
    26  
    27  func TestBluecatNewGatewayClient(t *testing.T) {
    28  	testCookie := http.Cookie{Name: "testCookie", Value: "exampleCookie"}
    29  	testToken := "exampleToken"
    30  	testgateWayHost := "exampleHost"
    31  	testDNSConfiguration := "exampleDNSConfiguration"
    32  	testDNSServer := "exampleServer"
    33  	testView := "testView"
    34  	testZone := "example.com"
    35  	testVerify := true
    36  
    37  	client := NewGatewayClientConfig(testCookie, testToken, testgateWayHost, testDNSConfiguration, testView, testZone, testDNSServer, testVerify)
    38  
    39  	if client.Cookie.Value != testCookie.Value || client.Cookie.Name != testCookie.Name || client.Token != testToken || client.Host != testgateWayHost || client.DNSConfiguration != testDNSConfiguration || client.View != testView || client.RootZone != testZone || client.SkipTLSVerify != testVerify {
    40  		t.Fatal("Client values dont match")
    41  	}
    42  }
    43  
    44  func TestBluecatExpandZones(t *testing.T) {
    45  	tests := map[string]struct {
    46  		input string
    47  		want  string
    48  	}{
    49  		"with subdomain":        {input: "example.com", want: "zones/com/zones/example/zones/"},
    50  		"only top level domain": {input: "com", want: "zones/com/zones/"},
    51  	}
    52  
    53  	for name, tc := range tests {
    54  		t.Run(name, func(t *testing.T) {
    55  			got := expandZone(tc.input)
    56  			diff := cmp.Diff(tc.want, got)
    57  			if diff != "" {
    58  				t.Fatalf(diff)
    59  			}
    60  		})
    61  	}
    62  }
    63  
    64  func TestBluecatValidDeployTypes(t *testing.T) {
    65  	validTypes := []string{"no-deploy", "full-deploy"}
    66  	invalidTypes := []string{"anything-else"}
    67  	for _, i := range validTypes {
    68  		if !IsValidDNSDeployType(i) {
    69  			t.Fatalf("%s should be a valid deploy type", i)
    70  		}
    71  	}
    72  	for _, i := range invalidTypes {
    73  		if IsValidDNSDeployType(i) {
    74  			t.Fatalf("%s should be a invalid deploy type", i)
    75  		}
    76  	}
    77  }
    78  
    79  // TODO: Add error checking in case "properties" are not properly formatted
    80  // Example test case... "invalid": {input: "abcde", want: map[string]string{}, err: InvalidProperty},
    81  func TestBluecatSplitProperties(t *testing.T) {
    82  	tests := map[string]struct {
    83  		input string
    84  		want  map[string]string
    85  	}{
    86  		"simple": {input: "ab=cd|ef=gh", want: map[string]string{"ab": "cd", "ef": "gh"}},
    87  	}
    88  
    89  	for name, tc := range tests {
    90  		t.Run(name, func(t *testing.T) {
    91  			got := SplitProperties(tc.input)
    92  			diff := cmp.Diff(tc.want, got)
    93  			if diff != "" {
    94  				t.Fatalf(diff)
    95  			}
    96  		})
    97  	}
    98  }
    99  
   100  func TestCreateTXTRecord(t *testing.T) {
   101  	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   102  		req := BluecatCreateTXTRecordRequest{}
   103  		requestBodyBytes, _ := io.ReadAll(r.Body)
   104  		err := json.Unmarshal(requestBodyBytes, &req)
   105  		if err != nil {
   106  			t.Fatalf("failed to unmarshal body for server full deploy")
   107  		}
   108  		if req.AbsoluteName == "alreadyexists.test.com" {
   109  			w.WriteHeader(http.StatusInternalServerError)
   110  		} else {
   111  			w.WriteHeader(http.StatusCreated)
   112  		}
   113  	}))
   114  	defer server.Close()
   115  
   116  	tests := map[string]struct {
   117  		config      GatewayClientConfig
   118  		zone        string
   119  		record      BluecatCreateTXTRecordRequest
   120  		expectError bool
   121  	}{
   122  		"simple-success": {GatewayClientConfig{Host: server.URL}, "test.com", BluecatCreateTXTRecordRequest{AbsoluteName: "my.test.com", Text: "here is my text"}, false},
   123  		"simple-failure": {GatewayClientConfig{Host: server.URL}, "test.com", BluecatCreateTXTRecordRequest{AbsoluteName: "alreadyexists.test.com", Text: "here is my text"}, true},
   124  	}
   125  
   126  	for name, tc := range tests {
   127  		t.Run(name, func(t *testing.T) {
   128  			got := tc.config.CreateTXTRecord(tc.zone, &tc.record)
   129  			if got != nil && !tc.expectError {
   130  				t.Fatalf("expected error %v, received error %v", tc.expectError, got)
   131  			}
   132  		})
   133  	}
   134  }
   135  
   136  func TestGetTXTRecord(t *testing.T) {
   137  	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   138  		if strings.Contains(r.RequestURI, "doesnotexist") {
   139  			w.WriteHeader(http.StatusNotFound)
   140  		} else {
   141  			w.WriteHeader(http.StatusOK)
   142  		}
   143  	}))
   144  	defer server.Close()
   145  
   146  	tests := map[string]struct {
   147  		config      GatewayClientConfig
   148  		name        string
   149  		expectError bool
   150  	}{
   151  		"simple-success": {GatewayClientConfig{Host: server.URL}, "mytxtrecord", false},
   152  		"simple-failure": {GatewayClientConfig{Host: server.URL}, "doesnotexist", true},
   153  	}
   154  
   155  	for name, tc := range tests {
   156  		t.Run(name, func(t *testing.T) {
   157  			record := BluecatTXTRecord{}
   158  			got := tc.config.GetTXTRecord(tc.name, &record)
   159  			if got != nil && !tc.expectError {
   160  				t.Fatalf("expected error %v, received error %v", tc.expectError, got)
   161  			}
   162  		})
   163  	}
   164  }
   165  
   166  func TestDeleteTXTRecord(t *testing.T) {
   167  	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   168  		if strings.Contains(r.RequestURI, "doesnotexist") {
   169  			w.WriteHeader(http.StatusBadRequest)
   170  		} else {
   171  			w.WriteHeader(http.StatusNoContent)
   172  		}
   173  	}))
   174  	defer server.Close()
   175  
   176  	tests := map[string]struct {
   177  		config      GatewayClientConfig
   178  		name        string
   179  		zone        string
   180  		expectError bool
   181  	}{
   182  		"simple-success": {GatewayClientConfig{Host: server.URL}, "todelete", "test.com", false},
   183  		"simple-failure": {GatewayClientConfig{Host: server.URL}, "doesnotexist", "test.com", true},
   184  	}
   185  
   186  	for name, tc := range tests {
   187  		t.Run(name, func(t *testing.T) {
   188  			got := tc.config.DeleteTXTRecord(tc.name, tc.zone)
   189  			if got != nil && !tc.expectError {
   190  				t.Fatalf("expected error %v, received error %v", tc.expectError, got)
   191  			}
   192  		})
   193  	}
   194  }
   195  
   196  func TestServerFullDeploy(t *testing.T) {
   197  	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   198  		req := BluecatServerFullDeployRequest{}
   199  		requestBodyBytes, _ := io.ReadAll(r.Body)
   200  		err := json.Unmarshal(requestBodyBytes, &req)
   201  		if err != nil {
   202  			t.Fatalf("failed to unmarshal body for server full deploy")
   203  		}
   204  		if req.ServerName == "serverdoesnotexist" {
   205  			w.WriteHeader(http.StatusNotFound)
   206  		} else {
   207  			w.WriteHeader(http.StatusCreated)
   208  		}
   209  	}))
   210  	defer server.Close()
   211  
   212  	tests := map[string]struct {
   213  		config      GatewayClientConfig
   214  		expectError bool
   215  	}{
   216  		"simple-success": {GatewayClientConfig{Host: server.URL, DNSServerName: "myserver"}, false},
   217  		"simple-failure": {GatewayClientConfig{Host: server.URL, DNSServerName: "serverdoesnotexist"}, true},
   218  	}
   219  
   220  	for name, tc := range tests {
   221  		t.Run(name, func(t *testing.T) {
   222  			got := tc.config.ServerFullDeploy()
   223  			if got != nil && !tc.expectError {
   224  				t.Fatalf("expected error %v, received error %v", tc.expectError, got)
   225  			}
   226  		})
   227  	}
   228  }