check_test.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http/httptest"
  7. "os"
  8. "path"
  9. "path/filepath"
  10. "strconv"
  11. "sync"
  12. "syscall"
  13. "testing"
  14. "time"
  15. "github.com/docker/docker/integration-cli/cli"
  16. "github.com/docker/docker/integration-cli/daemon"
  17. "github.com/docker/docker/integration-cli/environment"
  18. testdaemon "github.com/docker/docker/internal/test/daemon"
  19. ienv "github.com/docker/docker/internal/test/environment"
  20. "github.com/docker/docker/internal/test/fakestorage"
  21. "github.com/docker/docker/internal/test/fixtures/plugin"
  22. "github.com/docker/docker/internal/test/registry"
  23. "github.com/docker/docker/pkg/reexec"
  24. "github.com/go-check/check"
  25. "gotest.tools/assert"
  26. )
  27. const (
  28. // the private registry to use for tests
  29. privateRegistryURL = registry.DefaultURL
  30. // path to containerd's ctr binary
  31. ctrBinary = "ctr"
  32. // the docker daemon binary to use
  33. dockerdBinary = "dockerd"
  34. )
  35. var (
  36. testEnv *environment.Execution
  37. // the docker client binary to use
  38. dockerBinary = ""
  39. )
  40. func init() {
  41. var err error
  42. reexec.Init() // This is required for external graphdriver tests
  43. testEnv, err = environment.New()
  44. if err != nil {
  45. fmt.Println(err)
  46. os.Exit(1)
  47. }
  48. }
  49. func TestMain(m *testing.M) {
  50. dockerBinary = testEnv.DockerBinary()
  51. err := ienv.EnsureFrozenImagesLinux(&testEnv.Execution)
  52. if err != nil {
  53. fmt.Println(err)
  54. os.Exit(1)
  55. }
  56. testEnv.Print()
  57. os.Exit(m.Run())
  58. }
  59. func Test(t *testing.T) {
  60. cli.SetTestEnvironment(testEnv)
  61. fakestorage.SetTestEnvironment(&testEnv.Execution)
  62. ienv.ProtectAll(t, &testEnv.Execution)
  63. /*check.TestingT(t)*/
  64. }
  65. func init() {
  66. /*check.Suite(&DockerSuite{})*/
  67. }
  68. type DockerSuite struct {
  69. }
  70. func (s *DockerSuite) OnTimeout(c *testing.T) {
  71. if testEnv.IsRemoteDaemon() {
  72. return
  73. }
  74. path := filepath.Join(os.Getenv("DEST"), "docker.pid")
  75. b, err := ioutil.ReadFile(path)
  76. if err != nil {
  77. c.Fatalf("Failed to get daemon PID from %s\n", path)
  78. }
  79. rawPid, err := strconv.ParseInt(string(b), 10, 32)
  80. if err != nil {
  81. c.Fatalf("Failed to parse pid from %s: %s\n", path, err)
  82. }
  83. daemonPid := int(rawPid)
  84. if daemonPid > 0 {
  85. testdaemon.SignalDaemonDump(daemonPid)
  86. }
  87. }
  88. func (s *DockerSuite) TearDownTest(c *testing.T) {
  89. testEnv.Clean(c)
  90. }
  91. func init() {
  92. /*check.Suite(&DockerRegistrySuite{ds: &DockerSuite{}})*/
  93. }
  94. type DockerRegistrySuite struct {
  95. ds *DockerSuite
  96. reg *registry.V2
  97. d *daemon.Daemon
  98. }
  99. func (s *DockerRegistrySuite) OnTimeout(c *testing.T) {
  100. s.d.DumpStackAndQuit()
  101. }
  102. func (s *DockerRegistrySuite) SetUpTest(c *testing.T) {
  103. testRequires(c, DaemonIsLinux, RegistryHosting, testEnv.IsLocalDaemon)
  104. s.reg = registry.NewV2(c)
  105. s.reg.WaitReady(c)
  106. s.d = daemon.New(c, dockerBinary, dockerdBinary, testdaemon.WithEnvironment(testEnv.Execution))
  107. }
  108. func (s *DockerRegistrySuite) TearDownTest(c *testing.T) {
  109. if s.reg != nil {
  110. s.reg.Close()
  111. }
  112. if s.d != nil {
  113. s.d.Stop(c)
  114. }
  115. s.ds.TearDownTest(c)
  116. }
  117. func init() {
  118. /*check.Suite(&DockerSchema1RegistrySuite{ds: &DockerSuite{}})*/
  119. }
  120. type DockerSchema1RegistrySuite struct {
  121. ds *DockerSuite
  122. reg *registry.V2
  123. d *daemon.Daemon
  124. }
  125. func (s *DockerSchema1RegistrySuite) OnTimeout(c *testing.T) {
  126. s.d.DumpStackAndQuit()
  127. }
  128. func (s *DockerSchema1RegistrySuite) SetUpTest(c *testing.T) {
  129. testRequires(c, DaemonIsLinux, RegistryHosting, NotArm64, testEnv.IsLocalDaemon)
  130. s.reg = registry.NewV2(c, registry.Schema1)
  131. s.reg.WaitReady(c)
  132. s.d = daemon.New(c, dockerBinary, dockerdBinary, testdaemon.WithEnvironment(testEnv.Execution))
  133. }
  134. func (s *DockerSchema1RegistrySuite) TearDownTest(c *testing.T) {
  135. if s.reg != nil {
  136. s.reg.Close()
  137. }
  138. if s.d != nil {
  139. s.d.Stop(c)
  140. }
  141. s.ds.TearDownTest(c)
  142. }
  143. func init() {
  144. /*check.Suite(&DockerRegistryAuthHtpasswdSuite{ds: &DockerSuite{}})*/
  145. }
  146. type DockerRegistryAuthHtpasswdSuite struct {
  147. ds *DockerSuite
  148. reg *registry.V2
  149. d *daemon.Daemon
  150. }
  151. func (s *DockerRegistryAuthHtpasswdSuite) OnTimeout(c *testing.T) {
  152. s.d.DumpStackAndQuit()
  153. }
  154. func (s *DockerRegistryAuthHtpasswdSuite) SetUpTest(c *testing.T) {
  155. testRequires(c, DaemonIsLinux, RegistryHosting, testEnv.IsLocalDaemon)
  156. s.reg = registry.NewV2(c, registry.Htpasswd)
  157. s.reg.WaitReady(c)
  158. s.d = daemon.New(c, dockerBinary, dockerdBinary, testdaemon.WithEnvironment(testEnv.Execution))
  159. }
  160. func (s *DockerRegistryAuthHtpasswdSuite) TearDownTest(c *testing.T) {
  161. if s.reg != nil {
  162. out, err := s.d.Cmd("logout", privateRegistryURL)
  163. assert.NilError(c, err, out)
  164. s.reg.Close()
  165. }
  166. if s.d != nil {
  167. s.d.Stop(c)
  168. }
  169. s.ds.TearDownTest(c)
  170. }
  171. func init() {
  172. /*check.Suite(&DockerRegistryAuthTokenSuite{ds: &DockerSuite{}})*/
  173. }
  174. type DockerRegistryAuthTokenSuite struct {
  175. ds *DockerSuite
  176. reg *registry.V2
  177. d *daemon.Daemon
  178. }
  179. func (s *DockerRegistryAuthTokenSuite) OnTimeout(c *testing.T) {
  180. s.d.DumpStackAndQuit()
  181. }
  182. func (s *DockerRegistryAuthTokenSuite) SetUpTest(c *testing.T) {
  183. testRequires(c, DaemonIsLinux, RegistryHosting, testEnv.IsLocalDaemon)
  184. s.d = daemon.New(c, dockerBinary, dockerdBinary, testdaemon.WithEnvironment(testEnv.Execution))
  185. }
  186. func (s *DockerRegistryAuthTokenSuite) TearDownTest(c *testing.T) {
  187. if s.reg != nil {
  188. out, err := s.d.Cmd("logout", privateRegistryURL)
  189. assert.NilError(c, err, out)
  190. s.reg.Close()
  191. }
  192. if s.d != nil {
  193. s.d.Stop(c)
  194. }
  195. s.ds.TearDownTest(c)
  196. }
  197. func (s *DockerRegistryAuthTokenSuite) setupRegistryWithTokenService(c *testing.T, tokenURL string) {
  198. if s == nil {
  199. c.Fatal("registry suite isn't initialized")
  200. }
  201. s.reg = registry.NewV2(c, registry.Token(tokenURL))
  202. s.reg.WaitReady(c)
  203. }
  204. func init() {
  205. /*check.Suite(&DockerDaemonSuite{ds: &DockerSuite{}})*/
  206. }
  207. type DockerDaemonSuite struct {
  208. ds *DockerSuite
  209. d *daemon.Daemon
  210. }
  211. func (s *DockerDaemonSuite) OnTimeout(c *testing.T) {
  212. s.d.DumpStackAndQuit()
  213. }
  214. func (s *DockerDaemonSuite) SetUpTest(c *testing.T) {
  215. testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
  216. s.d = daemon.New(c, dockerBinary, dockerdBinary, testdaemon.WithEnvironment(testEnv.Execution))
  217. }
  218. func (s *DockerDaemonSuite) TearDownTest(c *testing.T) {
  219. testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
  220. if s.d != nil {
  221. s.d.Stop(c)
  222. }
  223. s.ds.TearDownTest(c)
  224. }
  225. func (s *DockerDaemonSuite) TearDownSuite(c *testing.T) {
  226. filepath.Walk(testdaemon.SockRoot, func(path string, fi os.FileInfo, err error) error {
  227. if err != nil {
  228. // ignore errors here
  229. // not cleaning up sockets is not really an error
  230. return nil
  231. }
  232. if fi.Mode() == os.ModeSocket {
  233. syscall.Unlink(path)
  234. }
  235. return nil
  236. })
  237. os.RemoveAll(testdaemon.SockRoot)
  238. }
  239. const defaultSwarmPort = 2477
  240. func init() {
  241. /*check.Suite(&DockerSwarmSuite{ds: &DockerSuite{}})*/
  242. }
  243. type DockerSwarmSuite struct {
  244. server *httptest.Server
  245. ds *DockerSuite
  246. daemonsLock sync.Mutex // protect access to daemons and portIndex
  247. daemons []*daemon.Daemon
  248. portIndex int
  249. }
  250. func (s *DockerSwarmSuite) OnTimeout(c *testing.T) {
  251. s.daemonsLock.Lock()
  252. defer s.daemonsLock.Unlock()
  253. for _, d := range s.daemons {
  254. d.DumpStackAndQuit()
  255. }
  256. }
  257. func (s *DockerSwarmSuite) SetUpTest(c *testing.T) {
  258. testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
  259. }
  260. func (s *DockerSwarmSuite) AddDaemon(c *testing.T, joinSwarm, manager bool) *daemon.Daemon {
  261. d := daemon.New(c, dockerBinary, dockerdBinary,
  262. testdaemon.WithEnvironment(testEnv.Execution),
  263. testdaemon.WithSwarmPort(defaultSwarmPort+s.portIndex),
  264. )
  265. if joinSwarm {
  266. if len(s.daemons) > 0 {
  267. d.StartAndSwarmJoin(c, s.daemons[0].Daemon, manager)
  268. } else {
  269. d.StartAndSwarmInit(c)
  270. }
  271. } else {
  272. d.StartNode(c)
  273. }
  274. s.daemonsLock.Lock()
  275. s.portIndex++
  276. s.daemons = append(s.daemons, d)
  277. s.daemonsLock.Unlock()
  278. return d
  279. }
  280. func (s *DockerSwarmSuite) TearDownTest(c *testing.T) {
  281. testRequires(c, DaemonIsLinux)
  282. s.daemonsLock.Lock()
  283. for _, d := range s.daemons {
  284. if d != nil {
  285. d.Stop(c)
  286. d.Cleanup(c)
  287. }
  288. }
  289. s.daemons = nil
  290. s.portIndex = 0
  291. s.daemonsLock.Unlock()
  292. s.ds.TearDownTest(c)
  293. }
  294. func init() {
  295. /*check.Suite(&DockerPluginSuite{ds: &DockerSuite{}})*/
  296. }
  297. type DockerPluginSuite struct {
  298. ds *DockerSuite
  299. registry *registry.V2
  300. }
  301. func (ps *DockerPluginSuite) registryHost() string {
  302. return privateRegistryURL
  303. }
  304. func (ps *DockerPluginSuite) getPluginRepo() string {
  305. return path.Join(ps.registryHost(), "plugin", "basic")
  306. }
  307. func (ps *DockerPluginSuite) getPluginRepoWithTag() string {
  308. return ps.getPluginRepo() + ":" + "latest"
  309. }
  310. func (ps *DockerPluginSuite) SetUpSuite(c *testing.T) {
  311. testRequires(c, DaemonIsLinux, RegistryHosting)
  312. ps.registry = registry.NewV2(c)
  313. ps.registry.WaitReady(c)
  314. ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
  315. defer cancel()
  316. err := plugin.CreateInRegistry(ctx, ps.getPluginRepo(), nil)
  317. assert.NilError(c, err, "failed to create plugin")
  318. }
  319. func (ps *DockerPluginSuite) TearDownSuite(c *testing.T) {
  320. if ps.registry != nil {
  321. ps.registry.Close()
  322. }
  323. }
  324. func (ps *DockerPluginSuite) TearDownTest(c *testing.T) {
  325. ps.ds.TearDownTest(c)
  326. }
  327. func (ps *DockerPluginSuite) OnTimeout(c *testing.T) {
  328. ps.ds.OnTimeout(c)
  329. }