github.com/aiven/aiven-go-client@v1.36.0/transit_gateway_vpc_attachment_test.go (about)

     1  package aiven
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  	"net/http/httptest"
     7  	"reflect"
     8  	"testing"
     9  )
    10  
    11  func setupTransitGatewayVPCAttachmentTestCase(t *testing.T) (*Client, func(t *testing.T)) {
    12  	t.Log("setup Service test case")
    13  
    14  	const (
    15  		UserName     = "test@aiven.io"
    16  		UserPassword = "testabcd"
    17  		AccessToken  = "some-random-token"
    18  	)
    19  
    20  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    21  		if r.URL.Path == "/userauth" {
    22  			w.Header().Set("Content-Type", "application/json")
    23  			w.WriteHeader(http.StatusOK)
    24  			err := json.NewEncoder(w).Encode(authResponse{
    25  				Token: AccessToken,
    26  				State: "active",
    27  			})
    28  
    29  			if err != nil {
    30  				t.Error(err)
    31  			}
    32  			return
    33  		}
    34  
    35  		vpc := VPC{
    36  			CloudName:    "aws-eu-west-1",
    37  			NetworkCIDR:  "172.16.0.0/24",
    38  			ProjectVPCID: "test-vpc-id",
    39  			State:        "ACTIVE",
    40  		}
    41  
    42  		t.Log(r.URL.Path)
    43  
    44  		if r.URL.Path == "/project/test-pr/vpcs/test-vpc-id/user-peer-network-cidrs" {
    45  			w.Header().Set("Content-Type", "application/json")
    46  
    47  			if r.Method == http.MethodPut {
    48  				var req TransitGatewayVPCAttachmentRequest
    49  
    50  				err := json.NewDecoder(r.Body).Decode(&req)
    51  				if err != nil {
    52  					t.Error(err)
    53  				}
    54  				if req.Add == nil {
    55  					errorResponse(t, w, http.StatusBadRequest, "Invalid input for add: None is not of type 'array'")
    56  					return
    57  				}
    58  				if req.Delete == nil {
    59  					errorResponse(t, w, http.StatusBadRequest, "Invalid input for delete: None is not of type 'array'")
    60  					return
    61  				}
    62  				if len(req.Add) == 0 && len(req.Delete) == 0 {
    63  					errorResponse(t, w, http.StatusBadRequest, "Both add and delete cidr sets cannot be empty")
    64  					return
    65  				}
    66  				for _, a := range req.Add {
    67  					if a.PeerResourceGroup != nil && *a.PeerResourceGroup == "" {
    68  						errorResponse(t, w, http.StatusBadRequest, "peer_resource_group must always be None for cloud aws-eu-west-1")
    69  						return
    70  					}
    71  				}
    72  			}
    73  
    74  			w.WriteHeader(http.StatusOK)
    75  
    76  			err := json.NewEncoder(w).Encode(vpc)
    77  
    78  			if err != nil {
    79  				t.Error(err)
    80  			}
    81  			return
    82  		}
    83  
    84  	}))
    85  
    86  	apiUrl = ts.URL
    87  
    88  	c, err := NewUserClient(UserName, UserPassword, "aiven-go-client-test/"+Version())
    89  	if err != nil {
    90  		t.Fatalf("user authentication error: %s", err)
    91  	}
    92  
    93  	return c, func(t *testing.T) {
    94  		t.Log("teardown TransitGatewayVPCAttachment test case")
    95  	}
    96  }
    97  
    98  func errorResponse(t *testing.T, w http.ResponseWriter, statusCode int, msg string) {
    99  	w.WriteHeader(statusCode)
   100  	err := json.NewEncoder(w).Encode(Error{
   101  		Status:  statusCode,
   102  		Message: msg,
   103  	})
   104  	if err != nil {
   105  		t.Error(err)
   106  	}
   107  }
   108  
   109  func TestTransitGatewayVPCAttachmentHandler_Update(t *testing.T) {
   110  	c, _ := setupTransitGatewayVPCAttachmentTestCase(t)
   111  
   112  	type fields struct {
   113  		client *Client
   114  	}
   115  	type args struct {
   116  		project      string
   117  		projectVPCId string
   118  		req          TransitGatewayVPCAttachmentRequest
   119  	}
   120  	tests := []struct {
   121  		name    string
   122  		fields  fields
   123  		args    args
   124  		want    *VPC
   125  		wantErr bool
   126  	}{
   127  		{
   128  			"bad-add",
   129  			fields{client: c},
   130  			args{
   131  				"test-pr",
   132  				"test-vpc-id",
   133  				TransitGatewayVPCAttachmentRequest{
   134  					Add:    nil,
   135  					Delete: []string{},
   136  				},
   137  			},
   138  			nil,
   139  			true,
   140  		},
   141  		{
   142  			"bad-del",
   143  			fields{client: c},
   144  			args{
   145  				"test-pr",
   146  				"test-vpc-id",
   147  				TransitGatewayVPCAttachmentRequest{
   148  					Add:    []TransitGatewayVPCAttachment{},
   149  					Delete: nil,
   150  				},
   151  			},
   152  			nil,
   153  			true,
   154  		},
   155  		{
   156  			"empty-add-del",
   157  			fields{client: c},
   158  			args{
   159  				"test-pr",
   160  				"test-vpc-id",
   161  				TransitGatewayVPCAttachmentRequest{
   162  					Add:    []TransitGatewayVPCAttachment{},
   163  					Delete: []string{},
   164  				},
   165  			},
   166  			nil,
   167  			true,
   168  		},
   169  		{
   170  			"empty-peer-resource-group",
   171  			fields{client: c},
   172  			args{
   173  				"test-pr",
   174  				"test-vpc-id",
   175  				TransitGatewayVPCAttachmentRequest{
   176  					Add: []TransitGatewayVPCAttachment{
   177  						{
   178  							CIDR:              "10.0.0.0/24",
   179  							PeerCloudAccount:  "111222333444",
   180  							PeerVPC:           "tgw-EXAMPLE",
   181  							PeerResourceGroup: ToStringPointer(""),
   182  						},
   183  					},
   184  					Delete: []string{},
   185  				},
   186  			},
   187  			nil,
   188  			true,
   189  		},
   190  		{
   191  			"normal",
   192  			fields{client: c},
   193  			args{
   194  				"test-pr",
   195  				"test-vpc-id",
   196  				TransitGatewayVPCAttachmentRequest{
   197  					Add: []TransitGatewayVPCAttachment{
   198  						{
   199  							CIDR:              "10.0.0.0/24",
   200  							PeerCloudAccount:  "111222333444",
   201  							PeerVPC:           "tgw-EXAMPLE",
   202  							PeerResourceGroup: nil,
   203  						},
   204  					},
   205  					Delete: []string{},
   206  				},
   207  			},
   208  			&VPC{
   209  				CloudName:    "aws-eu-west-1",
   210  				NetworkCIDR:  "172.16.0.0/24",
   211  				ProjectVPCID: "test-vpc-id",
   212  				State:        "ACTIVE",
   213  			},
   214  			false,
   215  		},
   216  	}
   217  
   218  	for _, tt := range tests {
   219  		t.Run(tt.name, func(t *testing.T) {
   220  			h := &TransitGatewayVPCAttachmentHandler{
   221  				client: tt.fields.client,
   222  			}
   223  			got, err := h.Update(tt.args.project, tt.args.projectVPCId, tt.args.req)
   224  			if (err != nil) != tt.wantErr {
   225  				t.Errorf("Update() error = %v, wantErr %v", err, tt.wantErr)
   226  				return
   227  			}
   228  			if !reflect.DeepEqual(got, tt.want) {
   229  				t.Errorf("Update() = %v, want %v", got, tt.want)
   230  			}
   231  		})
   232  	}
   233  }