You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.1 KiB

Unify `copies` and `artifacts` configuration Refactored `copies` configuration to allow for greater control over when and which both local build context files and variant artifacts are copied into the target image. The new configuration introduces a "local" keyword to signify when a `copies` entry should apply to the files from the local build context as opposed to files from another variant's image during a multi-stage build. variants: build: copies: - from: local source: ./src destination: . Note that with this change, the user must now explicitly define whether _any_ files should be copied in from the local build context. None will be copied in by default. To help keep configurations succinct, especially considering this new requirement, and to approximate the old `copies: variant`, a shorthand format and sane defaults for `source` and `destination` (depending whether `from` is "local" or a variant name) were implemented. variants: build: copies: [local] development: copies: - from: build - from: local source: ./config.dev.yaml destination: ./config.yaml The shorthand: copies: [ref, ...] # is equivalent to copies: [{ from: ref }, ...] And the following defaults are used when no `source` and `destination` are specified. copies: - from: local # defaults to - from: local source: . destination: . copies: - from: variant # defaults to two entries - from: variant source: /srv/app # the lives.in dir destination: /srv/app - from: variant source: /opt/local # the shared lib dir destination: /opt/local Bug: T211625 Change-Id: I4c4217905afc0762b6bd66ed594d43cc0486e3e2
6 years ago
  1. package config_test
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "gerrit.wikimedia.org/r/blubber/config"
  6. )
  7. func TestCopiesConfigAllArtifacts(t *testing.T) {
  8. cfg := config.CopiesConfig{
  9. {From: "foo"},
  10. {From: "local"},
  11. {From: "foo", Source: "./foo/dir", Destination: "./bar/dir"},
  12. }
  13. expanded := cfg.Expand("/app/dir")
  14. assert.Equal(t, config.CopiesConfig{
  15. {From: "foo", Source: "/app/dir", Destination: "/app/dir"},
  16. {From: "foo", Source: "/opt/lib", Destination: "/opt/lib"},
  17. {From: "local", Source: ".", Destination: "."},
  18. {From: "foo", Source: "./foo/dir", Destination: "./bar/dir"},
  19. }, expanded)
  20. }
  21. func TestCopiesConfigMerge(t *testing.T) {
  22. cfg := config.CopiesConfig{
  23. {From: "local"},
  24. {From: "foo", Source: "/app/dir", Destination: "/app"},
  25. {From: "bar"},
  26. }
  27. cfg.Merge(config.CopiesConfig{
  28. {From: "foo", Source: "/app/dir", Destination: "/app"},
  29. {From: "bar", Source: "/some/dir", Destination: "/dir"},
  30. })
  31. assert.Equal(t, config.CopiesConfig{
  32. {From: "local"},
  33. {From: "bar"},
  34. {From: "foo", Source: "/app/dir", Destination: "/app"},
  35. {From: "bar", Source: "/some/dir", Destination: "/dir"},
  36. }, cfg)
  37. }
  38. func TestCopiesConfigUnmarshalJSON(t *testing.T) {
  39. t.Run("strings", func(t *testing.T) {
  40. cfg := config.CopiesConfig{}
  41. err := cfg.UnmarshalJSON([]byte(`["foo", "bar"]`))
  42. if assert.NoError(t, err) {
  43. assert.Len(t, cfg, 2)
  44. assert.Equal(t, "foo", cfg[0].From)
  45. assert.Equal(t, "bar", cfg[1].From)
  46. }
  47. })
  48. t.Run("objects", func(t *testing.T) {
  49. cfg := config.CopiesConfig{}
  50. err := cfg.UnmarshalJSON([]byte(`[{ "from": "foo" }, { "from": "bar", "source": "/foo", "destination": "/bar" }]`))
  51. if assert.NoError(t, err) {
  52. assert.Len(t, cfg, 2)
  53. assert.Equal(t, "foo", cfg[0].From)
  54. assert.Equal(t, "bar", cfg[1].From)
  55. assert.Equal(t, "/foo", cfg[1].Source)
  56. assert.Equal(t, "/bar", cfg[1].Destination)
  57. }
  58. })
  59. }
  60. func TestCopiesConfigVariants(t *testing.T) {
  61. cfg := config.CopiesConfig{
  62. {From: "foo", Source: "/foo/src", Destination: "/foo/dst"},
  63. {From: "build", Source: "/foo/src", Destination: "/foo/dst"},
  64. {From: "foo"},
  65. }
  66. assert.Equal(t, []string{"foo", "build"}, cfg.Variants())
  67. }