snapshot_test: fix docker cleanup issue when test fail
Signed-off-by: yxxhero <aiopsclub@163.com>
This commit is contained in:
		
							parent
							
								
									7219273d3b
								
							
						
					
					
						commit
						1715e3e3a3
					
				|  | @ -4,6 +4,7 @@ import ( | ||||||
| 	"bufio" | 	"bufio" | ||||||
| 	"context" | 	"context" | ||||||
| 	"fmt" | 	"fmt" | ||||||
|  | 	"net/http" | ||||||
| 	"os" | 	"os" | ||||||
| 	"os/exec" | 	"os/exec" | ||||||
| 	"path/filepath" | 	"path/filepath" | ||||||
|  | @ -172,21 +173,29 @@ func testHelmfileTemplateWithBuildCommand(t *testing.T, goccyGoYaml bool) { | ||||||
| 			// run the docker registry v2 and push the test charts to the registry
 | 			// run the docker registry v2 and push the test charts to the registry
 | ||||||
| 			// so that it can be accessed by helm and helmfile as a oci registry based chart repository.
 | 			// so that it can be accessed by helm and helmfile as a oci registry based chart repository.
 | ||||||
| 			if config.LocalDockerRegistry.Enabled { | 			if config.LocalDockerRegistry.Enabled { | ||||||
| 				containerName := strings.Join([]string{"helmfile_docker_registry", name}, "_") | 				containerName := strings.Join([]string{ | ||||||
|  | 					"helmfile_docker_registry", | ||||||
|  | 					name, | ||||||
|  | 					strconv.FormatInt(time.Now().UnixNano(), 10)}, "_") | ||||||
| 
 | 
 | ||||||
| 				hostPort := config.LocalDockerRegistry.Port | 				hostPort := config.LocalDockerRegistry.Port | ||||||
| 				if hostPort <= 0 { | 				if hostPort <= 0 { | ||||||
| 					hostPort = 5000 | 					hostPort = 5000 | ||||||
| 				} | 				} | ||||||
| 
 | 
 | ||||||
| 				execDocker(t, "run", "--rm", "-d", "-p", fmt.Sprintf("%d:5000", hostPort), "--name", containerName, "registry:2") | 				containerPort := fmt.Sprintf("%d:5000", hostPort) | ||||||
| 				t.Cleanup(func() { |  | ||||||
| 					execDocker(t, "stop", containerName) |  | ||||||
| 				}) |  | ||||||
| 
 | 
 | ||||||
| 				// FIXME: this is a hack to wait for registry to be up and running
 | 				// Register cleanup BEFORE starting the container
 | ||||||
| 				// please replace with proper wait for registry
 | 				t.Cleanup(func() { | ||||||
| 				time.Sleep(5 * time.Second) | 					cmd := exec.Command("docker", "stop", containerName) | ||||||
|  | 					_ = cmd.Run() // Ignore errors during cleanup
 | ||||||
|  | 				}) | ||||||
|  | 				execDocker(t, "run", "--rm", "-d", "-p", containerPort, "--name", containerName, "registry:2") | ||||||
|  | 
 | ||||||
|  | 				// Wait for the registry to be ready instead of using time.Sleep
 | ||||||
|  | 				if err := waitForRegistry(t, fmt.Sprintf("localhost:%d", hostPort), 30*time.Second); err != nil { | ||||||
|  | 					t.Fatalf("Registry container failed to become ready: %v", err) | ||||||
|  | 				} | ||||||
| 
 | 
 | ||||||
| 				// We helm-package and helm-push every test chart saved in the ./testdata/charts directory
 | 				// We helm-package and helm-push every test chart saved in the ./testdata/charts directory
 | ||||||
| 				// to the local registry, so that they can be accessed by helmfile and helm invoked while testing.
 | 				// to the local registry, so that they can be accessed by helmfile and helm invoked while testing.
 | ||||||
|  | @ -376,3 +385,30 @@ func execHelm(t *testing.T, args ...string) string { | ||||||
| 
 | 
 | ||||||
| 	return string(out) | 	return string(out) | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | func waitForRegistry(t *testing.T, address string, timeout time.Duration) error { | ||||||
|  | 	t.Helper() | ||||||
|  | 
 | ||||||
|  |     client := http.Client{ | ||||||
|  |         Timeout: 1 * time.Second, | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     url := fmt.Sprintf("http://%s/v2/", address) | ||||||
|  |     deadline := time.Now().Add(timeout) | ||||||
|  | 
 | ||||||
|  |     for time.Now().Before(deadline) { | ||||||
|  |         resp, err := client.Get(url) | ||||||
|  |         if err == nil { | ||||||
|  |             resp.Body.Close() | ||||||
|  |             if resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusUnauthorized { | ||||||
|  |                 // Registry is up - a 200 OK or 401 Unauthorized both indicate the registry is running
 | ||||||
|  |                 return nil | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         // Wait a bit before trying again
 | ||||||
|  |         time.Sleep(500 * time.Millisecond) | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     return fmt.Errorf("timed out waiting for registry at %s after %s", address, timeout) | ||||||
|  | } | ||||||
		Loading…
	
		Reference in New Issue