@@ -3,8 +3,16 @@ package gitserver
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "github.com/cnoe-io/idpbuilder/pkg/kind"
7
+ "github.com/docker/docker/api/types/container"
8
+ "github.com/docker/go-connections/nat"
9
+ "io"
10
+ "net"
11
+ "os"
12
+ "strconv"
6
13
"strings"
7
14
"testing"
15
+ "time"
8
16
9
17
"github.com/cnoe-io/idpbuilder/api/v1alpha1"
10
18
"github.com/cnoe-io/idpbuilder/pkg/apps"
@@ -39,23 +47,74 @@ func TestReconcileGitServerImage(t *testing.T) {
39
47
},
40
48
}
41
49
42
- _ , err = r .reconcileGitServerImage (ctx , controllerruntime.Request {}, & resource )
50
+ dockerClient , err := docker .GetDockerClient ()
51
+ if err != nil {
52
+ t .Errorf ("Getting docker client: %v" , err )
53
+ }
54
+ defer dockerClient .Close ()
55
+ reader , err := dockerClient .ImagePull (ctx , "docker.io/library/registry:2" , types.ImagePullOptions {})
56
+ defer reader .Close ()
57
+ // blocks until pull is completed
58
+ io .Copy (os .Stdout , reader )
43
59
if err != nil {
44
- t .Errorf ( "reconcile error : %v" , err )
60
+ t .Fatalf ( "failed pulilng registry image : %v" , err )
45
61
}
46
62
47
- if ! strings .HasPrefix (resource .Status .ImageID , "sha256" ) {
48
- t .Errorf ("Invalid or no Image ID in status: %q" , resource .Status .ImageID )
63
+ waitTimeout := time .Second * 90
64
+ waitInterval := time .Second * 3
65
+ // very crude. no guarantee that the port will be available by the time request is sent to docker
66
+ endTime := time .Now ().Add (waitTimeout )
67
+ for {
68
+ if time .Now ().After (endTime ) {
69
+ t .Fatalf ("Timed out waiting for port %d to be available" , kind .ExposedRegistryPort )
70
+ }
71
+ conn , cErr := net .DialTimeout ("tcp" , net .JoinHostPort ("0.0.0.0" , strconv .Itoa (int (kind .ExposedRegistryPort ))), time .Second * 3 )
72
+ if cErr != nil {
73
+ break
74
+ }
75
+ conn .Close ()
76
+ time .Sleep (waitInterval )
49
77
}
50
78
51
- dockerClient , err := docker .GetDockerClient ()
79
+ resp , err := dockerClient .ContainerCreate (ctx , & container.Config {
80
+ Image : "docker.io/library/registry:2" ,
81
+ Tty : false ,
82
+ ExposedPorts : nat.PortSet {
83
+ nat .Port (fmt .Sprintf ("%d/tcp" , kind .InternalRegistryPort )): struct {}{},
84
+ },
85
+ }, & container.HostConfig {
86
+ PortBindings : nat.PortMap {
87
+ nat .Port (fmt .Sprintf ("%d/tcp" , kind .InternalRegistryPort )): []nat.PortBinding {
88
+ {
89
+ HostIP : "0.0.0.0" ,
90
+ HostPort : fmt .Sprintf ("%d" , kind .ExposedRegistryPort ),
91
+ },
92
+ },
93
+ },
94
+ }, nil , nil , "testcase-registry" )
52
95
if err != nil {
53
- t .Errorf ( "Getting docker client: %v" , err )
96
+ t .Fatalf ( "failed creating registry container %v" , err )
54
97
}
55
98
99
+ defer dockerClient .ContainerRemove (ctx , resp .ID , types.ContainerRemoveOptions {Force : true })
100
+
101
+ err = dockerClient .ContainerStart (ctx , resp .ID , types.ContainerStartOptions {})
102
+ if err != nil {
103
+ t .Fatalf ("failed starting container %v" , err )
104
+ }
105
+
106
+ _ , err = r .reconcileGitServerImage (ctx , controllerruntime.Request {}, & resource )
107
+ if err != nil {
108
+ t .Fatalf ("reconcile error: %v" , err )
109
+ }
110
+
111
+ if ! strings .HasPrefix (resource .Status .ImageID , "sha256" ) {
112
+ t .Fatalf ("Invalid or no Image ID in status: %q" , resource .Status .ImageID )
113
+ }
56
114
imageNameID := fmt .Sprintf ("%s@%s" , GetImageTag (& resource ), resource .Status .ImageID )
57
115
_ , err = dockerClient .ImageRemove (ctx , imageNameID , types.ImageRemoveOptions {})
58
116
if err != nil {
59
117
t .Errorf ("Removing docker image: %v" , err )
60
118
}
61
119
}
120
+
0 commit comments