github.com/seilagamo/poc-lava-release@v0.3.3-rc3/internal/engine/targetserver_test.go (about)

     1  // Copyright 2023 Adevinta
     2  
     3  package engine
     4  
     5  import (
     6  	"fmt"
     7  	"testing"
     8  
     9  	types "github.com/adevinta/vulcan-types"
    10  
    11  	"github.com/seilagamo/poc-lava-release/internal/config"
    12  )
    13  
    14  func TestGetTargetAddr(t *testing.T) {
    15  	tests := []struct {
    16  		name       string
    17  		target     config.Target
    18  		want       string
    19  		wantNilErr bool
    20  	}{
    21  		{
    22  			name: "IPv4",
    23  			target: config.Target{
    24  				AssetType:  types.IP,
    25  				Identifier: "127.0.0.1",
    26  			},
    27  			want:       "127.0.0.1",
    28  			wantNilErr: true,
    29  		},
    30  		{
    31  			name: "IPv6",
    32  			target: config.Target{
    33  				AssetType:  types.IP,
    34  				Identifier: "::1",
    35  			},
    36  			want:       "::1",
    37  			wantNilErr: true,
    38  		},
    39  		{
    40  			name: "Hostname",
    41  			target: config.Target{
    42  				AssetType:  types.Hostname,
    43  				Identifier: "example.com",
    44  			},
    45  			want:       "example.com",
    46  			wantNilErr: true,
    47  		},
    48  		{
    49  			name: "WebAddress port",
    50  			target: config.Target{
    51  				AssetType:  types.WebAddress,
    52  				Identifier: "https://example.com:1234/path",
    53  			},
    54  			want:       "example.com:1234",
    55  			wantNilErr: true,
    56  		},
    57  		{
    58  			name: "WebAddress scheme",
    59  			target: config.Target{
    60  				AssetType:  types.WebAddress,
    61  				Identifier: "https://example.com/path",
    62  			},
    63  			want:       "example.com:443",
    64  			wantNilErr: true,
    65  		},
    66  		{
    67  			name: "WebAddress unknown scheme",
    68  			target: config.Target{
    69  				AssetType:  types.WebAddress,
    70  				Identifier: "unknown://example.com/path",
    71  			},
    72  			want:       "example.com",
    73  			wantNilErr: true,
    74  		},
    75  		{
    76  			name: "invalid WebAddress",
    77  			target: config.Target{
    78  				AssetType:  types.WebAddress,
    79  				Identifier: "https://example.com:invalidport/path",
    80  			},
    81  			want:       "",
    82  			wantNilErr: false,
    83  		},
    84  		{
    85  			name: "GitRepository scp-like syntax",
    86  			target: config.Target{
    87  				AssetType:  types.GitRepository,
    88  				Identifier: "git@github.com:adevinta/lava.git",
    89  			},
    90  			want:       "github.com:22",
    91  			wantNilErr: true,
    92  		},
    93  		{
    94  			name: "GitRepository https",
    95  			target: config.Target{
    96  				AssetType:  types.GitRepository,
    97  				Identifier: "https://example.com/path/to/repo.git/",
    98  			},
    99  			want:       "example.com:443",
   100  			wantNilErr: true,
   101  		},
   102  		{
   103  			name: "GitRepository git",
   104  			target: config.Target{
   105  				AssetType:  types.GitRepository,
   106  				Identifier: "git://example.com/~user/path/to/repo.git/",
   107  			},
   108  			want:       "example.com:9418",
   109  			wantNilErr: true,
   110  		},
   111  		{
   112  			name: "GitRepository git port",
   113  			target: config.Target{
   114  				AssetType:  types.GitRepository,
   115  				Identifier: "git://example.com:443/~user/path/to/repo.git/",
   116  			},
   117  			want:       "example.com:443",
   118  			wantNilErr: true,
   119  		},
   120  		{
   121  			name: "invalid GitRepository URL",
   122  			target: config.Target{
   123  				AssetType:  types.GitRepository,
   124  				Identifier: "https://example.com:invalidport/path/to/repo.git/",
   125  			},
   126  			want:       "",
   127  			wantNilErr: false,
   128  		},
   129  		{
   130  			name: "invalid asset type",
   131  			target: config.Target{
   132  				AssetType:  types.IPRange,
   133  				Identifier: "127.0.0.1/8",
   134  			},
   135  			want:       "",
   136  			wantNilErr: false,
   137  		},
   138  		{
   139  			name: "GitRepository with empty host",
   140  			target: config.Target{
   141  				AssetType:  types.GitRepository,
   142  				Identifier: "/path",
   143  			},
   144  			want:       "",
   145  			wantNilErr: false,
   146  		},
   147  		{
   148  			name: "WebAddress with empty host",
   149  			target: config.Target{
   150  				AssetType:  types.WebAddress,
   151  				Identifier: "/path",
   152  			},
   153  			want:       "",
   154  			wantNilErr: false,
   155  		},
   156  	}
   157  
   158  	for _, tt := range tests {
   159  		t.Run(tt.name, func(t *testing.T) {
   160  			got, err := getTargetAddr(tt.target)
   161  
   162  			if (err == nil) != tt.wantNilErr {
   163  				t.Errorf("unexpected error: %v", err)
   164  			}
   165  
   166  			if got != tt.want {
   167  				t.Errorf("unexpected host: got: %v, want: %v", got, tt.want)
   168  			}
   169  		})
   170  	}
   171  }
   172  
   173  func TestMkIntIdentifier(t *testing.T) {
   174  	tests := []struct {
   175  		name       string
   176  		target     config.Target
   177  		want       string
   178  		wantNilErr bool
   179  	}{
   180  		{
   181  			name: "local IP",
   182  			target: config.Target{
   183  				Identifier: "127.0.0.1",
   184  				AssetType:  "IP",
   185  			},
   186  			want:       dockerInternalHost,
   187  			wantNilErr: true,
   188  		},
   189  		{
   190  			name: "remote IP",
   191  			target: config.Target{
   192  				Identifier: "192.168.1.1",
   193  				AssetType:  "IP",
   194  			},
   195  			want:       dockerInternalHost,
   196  			wantNilErr: true,
   197  		},
   198  		{
   199  			name: "local Hostname",
   200  			target: config.Target{
   201  				Identifier: "localhost",
   202  				AssetType:  "Hostname",
   203  			},
   204  			want:       dockerInternalHost,
   205  			wantNilErr: true,
   206  		},
   207  		{
   208  			name: "remote Hostname",
   209  			target: config.Target{
   210  				Identifier: "example.com",
   211  				AssetType:  "Hostname",
   212  			},
   213  			want:       dockerInternalHost,
   214  			wantNilErr: true,
   215  		},
   216  		{
   217  			name: "local WebAddress",
   218  			target: config.Target{
   219  				Identifier: "http://127.0.0.1:12345/path",
   220  				AssetType:  "WebAddress",
   221  			},
   222  			want:       fmt.Sprintf("http://%v:12345/path", dockerInternalHost),
   223  			wantNilErr: true,
   224  		},
   225  		{
   226  			name: "remote WebAddress",
   227  			target: config.Target{
   228  				Identifier: "http://192.168.1.1/path",
   229  				AssetType:  "WebAddress",
   230  			},
   231  			want:       fmt.Sprintf("http://%v/path", dockerInternalHost),
   232  			wantNilErr: true,
   233  		},
   234  		{
   235  			name: "local GitRepository",
   236  			target: config.Target{
   237  				Identifier: "ssh://git@localhost:12345/path/to/repo.git",
   238  				AssetType:  "GitRepository",
   239  			},
   240  			want:       fmt.Sprintf("ssh://git@%v:12345/path/to/repo.git", dockerInternalHost),
   241  			wantNilErr: true,
   242  		},
   243  		{
   244  			name: "remote GitRepository",
   245  			target: config.Target{
   246  				Identifier: "git@example.com:/path/to/repo.git",
   247  				AssetType:  "GitRepository",
   248  			},
   249  			want:       fmt.Sprintf("ssh://git@%v/path/to/repo.git", dockerInternalHost),
   250  			wantNilErr: true,
   251  		},
   252  		{
   253  			name: "multiple host occurrences",
   254  			target: config.Target{
   255  				Identifier: "localhost://localhost:12345/path",
   256  				AssetType:  "WebAddress",
   257  			},
   258  			want:       fmt.Sprintf("localhost://%v:12345/path", dockerInternalHost),
   259  			wantNilErr: true,
   260  		},
   261  		{
   262  			name: "DockerImage",
   263  			target: config.Target{
   264  				Identifier: "alpine:3.18",
   265  				AssetType:  "DockerImage",
   266  			},
   267  			want:       "",
   268  			wantNilErr: false,
   269  		},
   270  		{
   271  			name: "invalid GitRepository",
   272  			target: config.Target{
   273  				Identifier: "ssh://git@localhost:invalidport/path/to/repo.git",
   274  				AssetType:  "GitRepository",
   275  			},
   276  			want:       "",
   277  			wantNilErr: false,
   278  		},
   279  		{
   280  			name: "invalid WebAddress",
   281  			target: config.Target{
   282  				Identifier: "http://127.0.0.1:invalidport/path",
   283  				AssetType:  "WebAddress",
   284  			},
   285  			want:       "",
   286  			wantNilErr: false,
   287  		},
   288  	}
   289  
   290  	for _, tt := range tests {
   291  		t.Run(tt.name, func(t *testing.T) {
   292  			got, err := mkIntIdentifier(tt.target)
   293  
   294  			if (err == nil) != tt.wantNilErr {
   295  				t.Errorf("unexpected error: %v", err)
   296  			}
   297  
   298  			if got != tt.want {
   299  				t.Errorf("unexpected target: got: %v, want: %v", got, tt.want)
   300  			}
   301  		})
   302  	}
   303  }
   304  
   305  func TestParseGitURL(t *testing.T) {
   306  	tests := []struct {
   307  		name     string
   308  		url      string
   309  		wantHost string
   310  	}{
   311  		{
   312  			name:     "url",
   313  			url:      "https://example.com:443/path/to/repo.git/",
   314  			wantHost: "example.com:443",
   315  		},
   316  		{
   317  			name:     "scp long",
   318  			url:      "user@example.com:/~user/path/to/repo.git/",
   319  			wantHost: "example.com",
   320  		},
   321  		{
   322  			name:     "scp short",
   323  			url:      "example.com:/",
   324  			wantHost: "example.com",
   325  		},
   326  		{
   327  			name:     "local path",
   328  			url:      "/path/to/repo.git/",
   329  			wantHost: "",
   330  		},
   331  		{
   332  			name:     "scp with colon",
   333  			url:      "foo:bar",
   334  			wantHost: "foo",
   335  		},
   336  		{
   337  			name:     "local path with colon",
   338  			url:      "./foo:bar",
   339  			wantHost: "",
   340  		},
   341  		{
   342  			name:     "slash",
   343  			url:      "/",
   344  			wantHost: "",
   345  		},
   346  		{
   347  			name:     "colon",
   348  			url:      ":",
   349  			wantHost: "",
   350  		},
   351  		{
   352  			name:     "colon slash",
   353  			url:      ":/",
   354  			wantHost: "",
   355  		},
   356  		{
   357  			name:     "slash colon",
   358  			url:      "/:",
   359  			wantHost: "",
   360  		},
   361  	}
   362  
   363  	for _, tt := range tests {
   364  		t.Run(tt.name, func(t *testing.T) {
   365  			u, err := parseGitURL(tt.url)
   366  			if err != nil {
   367  				t.Fatalf("unexpected error: %v", err)
   368  			}
   369  
   370  			if u.Host != tt.wantHost {
   371  				t.Errorf("unexpected host: got: %v, want: %v)", u.Host, tt.wantHost)
   372  			}
   373  		})
   374  	}
   375  }