github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/pkg/systemd/generate/pods_test.go (about) 1 package generate 2 3 import ( 4 "testing" 5 6 "github.com/containers/podman/v2/pkg/domain/entities" 7 ) 8 9 func TestValidateRestartPolicyPod(t *testing.T) { 10 type podInfo struct { 11 restart string 12 } 13 tests := []struct { 14 name string 15 podInfo podInfo 16 wantErr bool 17 }{ 18 {"good-on", podInfo{restart: "no"}, false}, 19 {"good-on-success", podInfo{restart: "on-success"}, false}, 20 {"good-on-failure", podInfo{restart: "on-failure"}, false}, 21 {"good-on-abnormal", podInfo{restart: "on-abnormal"}, false}, 22 {"good-on-watchdog", podInfo{restart: "on-watchdog"}, false}, 23 {"good-on-abort", podInfo{restart: "on-abort"}, false}, 24 {"good-always", podInfo{restart: "always"}, false}, 25 {"fail", podInfo{restart: "foobar"}, true}, 26 {"failblank", podInfo{restart: ""}, true}, 27 } 28 for _, tt := range tests { 29 test := tt 30 t.Run(tt.name, func(t *testing.T) { 31 if err := validateRestartPolicy(test.podInfo.restart); (err != nil) != test.wantErr { 32 t.Errorf("ValidateRestartPolicy() error = %v, wantErr %v", err, test.wantErr) 33 } 34 }) 35 } 36 } 37 38 func TestCreatePodSystemdUnit(t *testing.T) { 39 podGood := `# pod-123abc.service 40 # autogenerated by Podman CI 41 42 [Unit] 43 Description=Podman pod-123abc.service 44 Documentation=man:podman-generate-systemd(1) 45 Wants=network.target 46 After=network-online.target 47 Requires=container-1.service container-2.service 48 Before=container-1.service container-2.service 49 50 [Service] 51 Environment=PODMAN_SYSTEMD_UNIT=%n 52 Restart=always 53 ExecStart=/usr/bin/podman start jadda-jadda-infra 54 ExecStop=/usr/bin/podman stop -t 10 jadda-jadda-infra 55 ExecStopPost=/usr/bin/podman stop -t 10 jadda-jadda-infra 56 PIDFile=/var/run/containers/storage/overlay-containers/639c53578af4d84b8800b4635fa4e680ee80fd67e0e6a2d4eea48d1e3230f401/userdata/conmon.pid 57 KillMode=none 58 Type=forking 59 60 [Install] 61 WantedBy=multi-user.target default.target 62 ` 63 64 podGoodNamedNew := `# pod-123abc.service 65 # autogenerated by Podman CI 66 67 [Unit] 68 Description=Podman pod-123abc.service 69 Documentation=man:podman-generate-systemd(1) 70 Wants=network.target 71 After=network-online.target 72 Requires=container-1.service container-2.service 73 Before=container-1.service container-2.service 74 75 [Service] 76 Environment=PODMAN_SYSTEMD_UNIT=%n 77 Restart=on-failure 78 ExecStartPre=/bin/rm -f %t/pod-123abc.pid %t/pod-123abc.pod-id 79 ExecStartPre=/usr/bin/podman pod create --infra-conmon-pidfile %t/pod-123abc.pid --pod-id-file %t/pod-123abc.pod-id --name foo "bar=arg with space" --replace 80 ExecStart=/usr/bin/podman pod start --pod-id-file %t/pod-123abc.pod-id 81 ExecStop=/usr/bin/podman pod stop --ignore --pod-id-file %t/pod-123abc.pod-id -t 10 82 ExecStopPost=/usr/bin/podman pod rm --ignore -f --pod-id-file %t/pod-123abc.pod-id 83 PIDFile=%t/pod-123abc.pid 84 KillMode=none 85 Type=forking 86 87 [Install] 88 WantedBy=multi-user.target default.target 89 ` 90 91 tests := []struct { 92 name string 93 info podInfo 94 want string 95 new bool 96 wantErr bool 97 }{ 98 {"pod", 99 podInfo{ 100 Executable: "/usr/bin/podman", 101 ServiceName: "pod-123abc", 102 InfraNameOrID: "jadda-jadda-infra", 103 RestartPolicy: "always", 104 PIDFile: "/var/run/containers/storage/overlay-containers/639c53578af4d84b8800b4635fa4e680ee80fd67e0e6a2d4eea48d1e3230f401/userdata/conmon.pid", 105 StopTimeout: 10, 106 PodmanVersion: "CI", 107 RequiredServices: []string{"container-1", "container-2"}, 108 }, 109 podGood, 110 false, 111 false, 112 }, 113 {"pod --new", 114 podInfo{ 115 Executable: "/usr/bin/podman", 116 ServiceName: "pod-123abc", 117 InfraNameOrID: "jadda-jadda-infra", 118 RestartPolicy: "on-failure", 119 PIDFile: "/var/run/containers/storage/overlay-containers/639c53578af4d84b8800b4635fa4e680ee80fd67e0e6a2d4eea48d1e3230f401/userdata/conmon.pid", 120 StopTimeout: 10, 121 PodmanVersion: "CI", 122 RequiredServices: []string{"container-1", "container-2"}, 123 CreateCommand: []string{"podman", "pod", "create", "--name", "foo", "bar=arg with space"}, 124 }, 125 podGoodNamedNew, 126 true, 127 false, 128 }, 129 } 130 131 for _, tt := range tests { 132 test := tt 133 t.Run(tt.name, func(t *testing.T) { 134 opts := entities.GenerateSystemdOptions{ 135 New: test.new, 136 } 137 got, err := executePodTemplate(&test.info, opts) 138 if (err != nil) != test.wantErr { 139 t.Errorf("CreatePodSystemdUnit() error = \n%v, wantErr \n%v", err, test.wantErr) 140 return 141 } 142 if got != test.want { 143 t.Errorf("CreatePodSystemdUnit() = \n%v\n---------> want\n%v", got, test.want) 144 } 145 }) 146 } 147 }