fix #2 and added some minimal tests
This commit is contained in:
		| @@ -1,4 +1,12 @@ | ||||
| steps: | ||||
|   test: | ||||
|     image: golang | ||||
|     commands: | ||||
|       - go test ./... | ||||
|     when: | ||||
|       event: | ||||
|         - push | ||||
|         - pull_request | ||||
|   build: | ||||
|     image: woodpeckerci/plugin-docker-buildx | ||||
|     settings: | ||||
|   | ||||
							
								
								
									
										25
									
								
								internal/config_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								internal/config_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,25 @@ | ||||
| package internal | ||||
|  | ||||
| import ( | ||||
| 	"reflect" | ||||
| 	"testing" | ||||
| ) | ||||
|  | ||||
| var defaultConfig = Config{ | ||||
| 	LogLevel:       "Info", | ||||
| 	LabelSelector:  "kops.k8s.io/instance-role=Node", | ||||
| 	Protocol:       "http", | ||||
| 	HcloudToken:    "", | ||||
| 	FloatingIPName: "", | ||||
| 	DryRun:         false, | ||||
| } | ||||
|  | ||||
| func TestConfigDefaults(t *testing.T) { | ||||
| 	cfg, err := GenConfig() | ||||
| 	if err != nil { | ||||
| 		t.Errorf("%s", err.Error()) | ||||
| 	} | ||||
| 	if !reflect.DeepEqual(&defaultConfig, cfg) { | ||||
| 		t.Errorf("got %+v, want %+v", cfg, defaultConfig) | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										23
									
								
								internal/health_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								internal/health_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,23 @@ | ||||
| package internal | ||||
|  | ||||
| import ( | ||||
| 	"net/http" | ||||
| 	"strings" | ||||
| 	"testing" | ||||
| ) | ||||
|  | ||||
| func TestHealth(t *testing.T) { | ||||
| 	go func() { | ||||
| 		StartHealthEndpoint() | ||||
| 	}() | ||||
| 	request, _ := http.NewRequest(http.MethodGet, "http://localhost:8080/health", strings.NewReader("")) | ||||
| 	resp, err := http.DefaultClient.Do(request) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		t.Errorf("Health endpoint did not start: %v", err) | ||||
| 	} | ||||
|  | ||||
| 	if resp.StatusCode != http.StatusOK { | ||||
| 		t.Errorf("Bad response from health endpoint. Want: %d, got %d", http.StatusOK, resp.StatusCode) | ||||
| 	} | ||||
| } | ||||
| @@ -78,6 +78,12 @@ func AttachFloatingIpToNode(cfg *Config, server hcloud.Server) error { | ||||
| 	if err != nil { | ||||
| 		return errors.New(fmt.Sprintf("Could not find Floating IP by name: %s", err.Error())) | ||||
| 	} | ||||
| 	if floatingIP.Server.ID == server.ID { | ||||
| 		log.WithFields(log.Fields{ | ||||
| 			"Caller": "AttachFloatingIpToNode", | ||||
| 		}).Info(fmt.Sprintf("Floating IP %s already assigned to Node %s", cfg.FloatingIPName, server.Name)) | ||||
| 		return nil | ||||
| 	} | ||||
| 	_, _, err = client.FloatingIP.Assign(context.TODO(), floatingIP, &server) | ||||
| 	if err != nil { | ||||
| 		log.WithFields(log.Fields{ | ||||
|   | ||||
		Reference in New Issue
	
	Block a user