github.com/joshdk/godel@v0.0.0-20170529232908-862138a45aee/apps/distgo/cmd/publish/publish_test.go (about)

     1  // Copyright 2016 Palantir Technologies, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package publish_test
    16  
    17  import (
    18  	"fmt"
    19  	"io/ioutil"
    20  	"os"
    21  	"path"
    22  	"runtime"
    23  	"testing"
    24  
    25  	"github.com/nmiyake/pkg/dirs"
    26  	"github.com/stretchr/testify/assert"
    27  	"github.com/stretchr/testify/require"
    28  
    29  	"github.com/palantir/godel/apps/distgo/cmd/build"
    30  	"github.com/palantir/godel/apps/distgo/cmd/dist"
    31  	"github.com/palantir/godel/apps/distgo/cmd/publish"
    32  	"github.com/palantir/godel/apps/distgo/params"
    33  	"github.com/palantir/godel/apps/distgo/pkg/git"
    34  	"github.com/palantir/godel/apps/distgo/pkg/git/gittest"
    35  	"github.com/palantir/godel/apps/distgo/pkg/osarch"
    36  )
    37  
    38  const (
    39  	testMain = `package main
    40  
    41  import "fmt"
    42  
    43  var testVersionVar = "defaultVersion"
    44  
    45  func main() {
    46  	fmt.Println(testVersionVar)
    47  }
    48  `
    49  )
    50  
    51  func TestPublishLocal(t *testing.T) {
    52  	tmp, cleanup, err := dirs.TempDir("", "")
    53  	defer cleanup()
    54  	require.NoError(t, err)
    55  
    56  	for i, currCase := range []struct {
    57  		name        string
    58  		buildSpec   func(projectDir string) params.ProductBuildSpecWithDeps
    59  		skip        func() bool
    60  		wantPaths   []string
    61  		wantContent map[string]string
    62  	}{
    63  		{
    64  			name: "local publish for SLS product",
    65  			buildSpec: func(projectDir string) params.ProductBuildSpecWithDeps {
    66  				specWithDeps, err := params.NewProductBuildSpecWithDeps(params.NewProductBuildSpec(projectDir, "publish-test-service", git.ProjectInfo{
    67  					Version:  "0.0.1",
    68  					Branch:   "0.0.1",
    69  					Revision: "0",
    70  				}, params.Product{
    71  					Build: params.Build{
    72  						MainPkg: "./.",
    73  					},
    74  					Publish: params.Publish{
    75  						GroupID: "com.palantir.distgo-publish-test",
    76  					},
    77  				}, params.Project{}), nil)
    78  				require.NoError(t, err)
    79  				return specWithDeps
    80  			},
    81  			wantPaths: []string{
    82  				"com/palantir/distgo-publish-test/publish-test-service/0.0.1/publish-test-service-0.0.1.pom",
    83  				"com/palantir/distgo-publish-test/publish-test-service/0.0.1/publish-test-service-0.0.1.sls.tgz",
    84  			},
    85  			wantContent: map[string]string{
    86  				"com/palantir/distgo-publish-test/publish-test-service/0.0.1/publish-test-service-0.0.1.pom": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
    87  					"<project xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\" xmlns=\"http://maven.apache.org/POM/4.0.0\"\nxmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n" +
    88  					"<modelVersion>4.0.0</modelVersion>\n" +
    89  					"<groupId>com.palantir.distgo-publish-test</groupId>\n" +
    90  					"<artifactId>publish-test-service</artifactId>\n" +
    91  					"<version>0.0.1</version>\n" +
    92  					"<packaging>sls.tgz</packaging>\n" +
    93  					"</project>\n",
    94  			},
    95  		},
    96  		{
    97  			name: "local publish for bin product",
    98  			buildSpec: func(projectDir string) params.ProductBuildSpecWithDeps {
    99  				specWithDeps, err := params.NewProductBuildSpecWithDeps(params.NewProductBuildSpec(projectDir, "publish-test-service", git.ProjectInfo{
   100  					Version:  "0.0.1",
   101  					Branch:   "0.0.1",
   102  					Revision: "0",
   103  				}, params.Product{
   104  					Build: params.Build{
   105  						MainPkg: "./.",
   106  					},
   107  					Dist: []params.Dist{{
   108  						Info: &params.BinDistInfo{},
   109  					}},
   110  					Publish: params.Publish{
   111  						GroupID: "com.palantir.distgo-publish-test",
   112  					},
   113  				}, params.Project{}), nil)
   114  				require.NoError(t, err)
   115  				return specWithDeps
   116  			},
   117  			wantPaths: []string{
   118  				"com/palantir/distgo-publish-test/publish-test-service/0.0.1/publish-test-service-0.0.1.pom",
   119  				"com/palantir/distgo-publish-test/publish-test-service/0.0.1/publish-test-service-0.0.1.tgz",
   120  			},
   121  			wantContent: map[string]string{
   122  				"com/palantir/distgo-publish-test/publish-test-service/0.0.1/publish-test-service-0.0.1.pom": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
   123  					"<project xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\" xmlns=\"http://maven.apache.org/POM/4.0.0\"\n" +
   124  					"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n" +
   125  					"<modelVersion>4.0.0</modelVersion>\n" +
   126  					"<groupId>com.palantir.distgo-publish-test</groupId>\n" +
   127  					"<artifactId>publish-test-service</artifactId>\n" +
   128  					"<version>0.0.1</version>\n" +
   129  					"<packaging>tgz</packaging>\n" +
   130  					"</project>\n",
   131  			},
   132  		},
   133  		{
   134  			name: "local publish for OSArch-bin product",
   135  			buildSpec: func(projectDir string) params.ProductBuildSpecWithDeps {
   136  				specWithDeps, err := params.NewProductBuildSpecWithDeps(params.NewProductBuildSpec(projectDir, "publish-test-service", git.ProjectInfo{
   137  					Version:  "0.0.1",
   138  					Branch:   "0.0.1",
   139  					Revision: "0",
   140  				}, params.Product{
   141  					Build: params.Build{
   142  						MainPkg: "./.",
   143  						OSArchs: []osarch.OSArch{
   144  							{
   145  								OS:   "darwin",
   146  								Arch: "amd64",
   147  							},
   148  						},
   149  					},
   150  					Dist: []params.Dist{{
   151  						Info: &params.OSArchsBinDistInfo{
   152  							OSArchs: []osarch.OSArch{
   153  								{
   154  									OS:   "darwin",
   155  									Arch: "amd64",
   156  								},
   157  							},
   158  						},
   159  					}},
   160  					Publish: params.Publish{
   161  						GroupID: "com.palantir.distgo-publish-test",
   162  					},
   163  				}, params.Project{}), nil)
   164  				require.NoError(t, err)
   165  				return specWithDeps
   166  			},
   167  			wantPaths: []string{
   168  				"com/palantir/distgo-publish-test/publish-test-service/0.0.1/publish-test-service-0.0.1.pom",
   169  				"com/palantir/distgo-publish-test/publish-test-service/0.0.1/publish-test-service-0.0.1-darwin-amd64.tgz",
   170  			},
   171  			wantContent: map[string]string{
   172  				"com/palantir/distgo-publish-test/publish-test-service/0.0.1/publish-test-service-0.0.1.pom": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
   173  					"<project xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\" xmlns=\"http://maven.apache.org/POM/4.0.0\"\n" +
   174  					"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n" +
   175  					"<modelVersion>4.0.0</modelVersion>\n" +
   176  					"<groupId>com.palantir.distgo-publish-test</groupId>\n" +
   177  					"<artifactId>publish-test-service</artifactId>\n" +
   178  					"<version>0.0.1</version>\n" +
   179  					"<packaging>tgz</packaging>\n" +
   180  					"</project>\n",
   181  			},
   182  		},
   183  		{
   184  			name: "local publish for OSArch-bin product with multiple targets creates multiple artifacts but single POM",
   185  			buildSpec: func(projectDir string) params.ProductBuildSpecWithDeps {
   186  				specWithDeps, err := params.NewProductBuildSpecWithDeps(params.NewProductBuildSpec(projectDir, "publish-test-service", git.ProjectInfo{
   187  					Version:  "0.0.1",
   188  					Branch:   "0.0.1",
   189  					Revision: "0",
   190  				}, params.Product{
   191  					Build: params.Build{
   192  						MainPkg: "./.",
   193  						OSArchs: []osarch.OSArch{
   194  							{
   195  								OS:   "darwin",
   196  								Arch: "amd64",
   197  							},
   198  							{
   199  								OS:   "linux",
   200  								Arch: "amd64",
   201  							},
   202  						},
   203  					},
   204  					Dist: []params.Dist{{
   205  						Info: &params.OSArchsBinDistInfo{
   206  							OSArchs: []osarch.OSArch{
   207  								{
   208  									OS:   "darwin",
   209  									Arch: "amd64",
   210  								},
   211  							},
   212  						},
   213  					}, {
   214  						Info: &params.OSArchsBinDistInfo{
   215  							OSArchs: []osarch.OSArch{
   216  								{
   217  									OS:   "linux",
   218  									Arch: "amd64",
   219  								},
   220  							},
   221  						},
   222  					}},
   223  					Publish: params.Publish{
   224  						GroupID: "com.palantir.distgo-publish-test",
   225  					},
   226  				}, params.Project{}), nil)
   227  				require.NoError(t, err)
   228  				return specWithDeps
   229  			},
   230  			wantPaths: []string{
   231  				"com/palantir/distgo-publish-test/publish-test-service/0.0.1/publish-test-service-0.0.1.pom",
   232  				"com/palantir/distgo-publish-test/publish-test-service/0.0.1/publish-test-service-0.0.1-darwin-amd64.tgz",
   233  				"com/palantir/distgo-publish-test/publish-test-service/0.0.1/publish-test-service-0.0.1-linux-amd64.tgz",
   234  			},
   235  			wantContent: map[string]string{
   236  				"com/palantir/distgo-publish-test/publish-test-service/0.0.1/publish-test-service-0.0.1.pom": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
   237  					"<project xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\" xmlns=\"http://maven.apache.org/POM/4.0.0\"\n" +
   238  					"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n" +
   239  					"<modelVersion>4.0.0</modelVersion>\n" +
   240  					"<groupId>com.palantir.distgo-publish-test</groupId>\n" +
   241  					"<artifactId>publish-test-service</artifactId>\n" +
   242  					"<version>0.0.1</version>\n" +
   243  					"<packaging>tgz</packaging>\n" +
   244  					"</project>\n",
   245  			},
   246  		},
   247  		{
   248  			name: "local publish for OSArch-bin product with dist with multiple OS/Archs creates multiple artifacts but single POM",
   249  			buildSpec: func(projectDir string) params.ProductBuildSpecWithDeps {
   250  				specWithDeps, err := params.NewProductBuildSpecWithDeps(params.NewProductBuildSpec(projectDir, "publish-test-service", git.ProjectInfo{
   251  					Version:  "0.0.1",
   252  					Branch:   "0.0.1",
   253  					Revision: "0",
   254  				}, params.Product{
   255  					Build: params.Build{
   256  						MainPkg: "./.",
   257  						OSArchs: []osarch.OSArch{
   258  							{
   259  								OS:   "darwin",
   260  								Arch: "amd64",
   261  							},
   262  							{
   263  								OS:   "linux",
   264  								Arch: "amd64",
   265  							},
   266  						},
   267  					},
   268  					Dist: []params.Dist{{
   269  						Info: &params.OSArchsBinDistInfo{
   270  							OSArchs: []osarch.OSArch{
   271  								{
   272  									OS:   "darwin",
   273  									Arch: "amd64",
   274  								},
   275  								{
   276  									OS:   "linux",
   277  									Arch: "amd64",
   278  								},
   279  							},
   280  						},
   281  					}},
   282  					Publish: params.Publish{
   283  						GroupID: "com.palantir.distgo-publish-test",
   284  					},
   285  				}, params.Project{}), nil)
   286  				require.NoError(t, err)
   287  				return specWithDeps
   288  			},
   289  			wantPaths: []string{
   290  				"com/palantir/distgo-publish-test/publish-test-service/0.0.1/publish-test-service-0.0.1.pom",
   291  				"com/palantir/distgo-publish-test/publish-test-service/0.0.1/publish-test-service-0.0.1-darwin-amd64.tgz",
   292  				"com/palantir/distgo-publish-test/publish-test-service/0.0.1/publish-test-service-0.0.1-linux-amd64.tgz",
   293  			},
   294  			wantContent: map[string]string{
   295  				"com/palantir/distgo-publish-test/publish-test-service/0.0.1/publish-test-service-0.0.1.pom": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
   296  					"<project xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\" xmlns=\"http://maven.apache.org/POM/4.0.0\"\n" +
   297  					"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n" +
   298  					"<modelVersion>4.0.0</modelVersion>\n" +
   299  					"<groupId>com.palantir.distgo-publish-test</groupId>\n" +
   300  					"<artifactId>publish-test-service</artifactId>\n" +
   301  					"<version>0.0.1</version>\n" +
   302  					"<packaging>tgz</packaging>\n" +
   303  					"</project>\n",
   304  			},
   305  		},
   306  		{
   307  			name: "local publish for manual dist product",
   308  			buildSpec: func(projectDir string) params.ProductBuildSpecWithDeps {
   309  				specWithDeps, err := params.NewProductBuildSpecWithDeps(params.NewProductBuildSpec(projectDir, "publish-test-service", git.ProjectInfo{
   310  					Version:  "0.0.1",
   311  					Branch:   "0.0.1",
   312  					Revision: "0",
   313  				}, params.Product{
   314  					Build: params.Build{
   315  						Skip: true,
   316  					},
   317  					Dist: []params.Dist{{
   318  						Script: `
   319  echo "test-dist-contents" > "$DIST_DIR/$PRODUCT-$VERSION.tgz"
   320  `,
   321  						Info: &params.ManualDistInfo{
   322  							Extension: "tgz",
   323  						},
   324  					}},
   325  					Publish: params.Publish{
   326  						GroupID: "com.palantir.distgo-publish-test",
   327  					},
   328  				}, params.Project{}), nil)
   329  				require.NoError(t, err)
   330  				return specWithDeps
   331  			},
   332  			wantPaths: []string{
   333  				"com/palantir/distgo-publish-test/publish-test-service/0.0.1/publish-test-service-0.0.1.pom",
   334  				"com/palantir/distgo-publish-test/publish-test-service/0.0.1/publish-test-service-0.0.1.tgz",
   335  			},
   336  			wantContent: map[string]string{
   337  				"com/palantir/distgo-publish-test/publish-test-service/0.0.1/publish-test-service-0.0.1.pom": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
   338  					"<project xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\" xmlns=\"http://maven.apache.org/POM/4.0.0\"\n" +
   339  					"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n" +
   340  					"<modelVersion>4.0.0</modelVersion>\n" +
   341  					"<groupId>com.palantir.distgo-publish-test</groupId>\n" +
   342  					"<artifactId>publish-test-service</artifactId>\n" +
   343  					"<version>0.0.1</version>\n" +
   344  					"<packaging>tgz</packaging>\n" +
   345  					"</project>\n",
   346  			},
   347  		},
   348  		{
   349  			name: "local publish for product with no distribution specified creates SLS",
   350  			buildSpec: func(projectDir string) params.ProductBuildSpecWithDeps {
   351  				specWithDeps, err := params.NewProductBuildSpecWithDeps(params.NewProductBuildSpec(projectDir, "publish-test-service", git.ProjectInfo{
   352  					Version:  "0.0.1",
   353  					Branch:   "0.0.1",
   354  					Revision: "0",
   355  				}, params.Product{
   356  					Build: params.Build{
   357  						MainPkg: "./.",
   358  					},
   359  					Publish: params.Publish{
   360  						GroupID: "com.palantir.distgo-publish-test",
   361  					},
   362  				}, params.Project{}), nil)
   363  				require.NoError(t, err)
   364  				return specWithDeps
   365  			},
   366  			wantPaths: []string{
   367  				"com/palantir/distgo-publish-test/publish-test-service/0.0.1/publish-test-service-0.0.1.pom",
   368  				"com/palantir/distgo-publish-test/publish-test-service/0.0.1/publish-test-service-0.0.1.sls.tgz",
   369  			},
   370  			wantContent: map[string]string{
   371  				"com/palantir/distgo-publish-test/publish-test-service/0.0.1/publish-test-service-0.0.1.pom": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
   372  					"<project xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\" xmlns=\"http://maven.apache.org/POM/4.0.0\"\n" +
   373  					"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n" +
   374  					"<modelVersion>4.0.0</modelVersion>\n" +
   375  					"<groupId>com.palantir.distgo-publish-test</groupId>\n" +
   376  					"<artifactId>publish-test-service</artifactId>\n" +
   377  					"<version>0.0.1</version>\n" +
   378  					"<packaging>sls.tgz</packaging>\n" +
   379  					"</project>\n",
   380  			},
   381  		},
   382  		{
   383  			name: "local publish for product with bin and RPM distributions",
   384  			buildSpec: func(projectDir string) params.ProductBuildSpecWithDeps {
   385  				specWithDeps, err := params.NewProductBuildSpecWithDeps(params.NewProductBuildSpec(projectDir, "test", git.ProjectInfo{
   386  					Version:  "0.0.1",
   387  					Branch:   "0.0.1",
   388  					Revision: "0",
   389  				}, params.Product{
   390  					Build: params.Build{
   391  						MainPkg: "./.",
   392  					},
   393  					Dist: []params.Dist{{
   394  						Info:      &params.BinDistInfo{},
   395  						OutputDir: "dist/bin",
   396  						Script:    "touch $DIST_DIR/dist-1.txt",
   397  					}, {
   398  						Info:      &params.RPMDistInfo{},
   399  						OutputDir: "dist/rpm",
   400  						Script:    "touch $DIST_DIR/dist-2.txt",
   401  						Publish: params.Publish{
   402  							GroupID: "com.palantir.pcloud-rpm",
   403  						},
   404  					}},
   405  					Publish: params.Publish{
   406  						GroupID: "com.palantir.pcloud-bin",
   407  					},
   408  				}, params.Project{}), nil)
   409  				require.NoError(t, err)
   410  				return specWithDeps
   411  			},
   412  			skip: func() bool {
   413  				// rpm dist type is currently only supported for linux-amd64
   414  				return !(runtime.GOOS == "linux" && runtime.GOARCH == "amd64")
   415  			},
   416  			wantPaths: []string{
   417  				"com/palantir/pcloud-bin/test/0.0.1/test-0.0.1.pom",
   418  				"com/palantir/pcloud-bin/test/0.0.1/test-0.0.1.tgz",
   419  				"com/palantir/pcloud-rpm/test/0.0.1/test-0.0.1.pom",
   420  				"com/palantir/pcloud-rpm/test/0.0.1/test-0.0.1-1.x86_64.rpm",
   421  			},
   422  			wantContent: map[string]string{
   423  				"com/palantir/pcloud-bin/test/0.0.1/test-0.0.1.pom": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<project xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\" xmlns=\"http://maven.apache.org/POM/4.0.0\"\nxmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n<modelVersion>4.0.0</modelVersion>\n<groupId>com.palantir.pcloud-bin</groupId>\n<artifactId>test</artifactId>\n<version>0.0.1</version>\n<packaging>tgz</packaging>\n</project>\n",
   424  				"com/palantir/pcloud-rpm/test/0.0.1/test-0.0.1.pom": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<project xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\" xmlns=\"http://maven.apache.org/POM/4.0.0\"\nxmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n<modelVersion>4.0.0</modelVersion>\n<groupId>com.palantir.pcloud-rpm</groupId>\n<artifactId>test</artifactId>\n<version>0.0.1</version>\n<packaging>rpm</packaging>\n</project>\n",
   425  			},
   426  		},
   427  	} {
   428  		if currCase.skip != nil && currCase.skip() {
   429  			fmt.Printf("Skipping case %d\n", i)
   430  			continue
   431  		}
   432  
   433  		currTmp, err := ioutil.TempDir(tmp, "")
   434  		require.NoError(t, err)
   435  
   436  		gittest.InitGitDir(t, currTmp)
   437  
   438  		err = ioutil.WriteFile(path.Join(currTmp, "main.go"), []byte(testMain), 0644)
   439  		require.NoError(t, err)
   440  
   441  		currSpecWithDeps := currCase.buildSpec(currTmp)
   442  
   443  		err = build.Run(build.RequiresBuild(currSpecWithDeps, nil).Specs(), nil, build.Context{}, ioutil.Discard)
   444  		require.NoError(t, err, "Case %d", i)
   445  
   446  		err = dist.Run(currSpecWithDeps, ioutil.Discard)
   447  		require.NoError(t, err, "Case %d", i)
   448  
   449  		repo := path.Join(currTmp, "repository")
   450  		err = publish.Run(currSpecWithDeps, &publish.LocalPublishInfo{
   451  			Path: repo,
   452  		}, nil, ioutil.Discard)
   453  		require.NoError(t, err, "Case %d", i)
   454  
   455  		for _, currPath := range currCase.wantPaths {
   456  			info, err := os.Stat(path.Join(repo, currPath))
   457  			require.NoError(t, err, "Case %d", i)
   458  			assert.False(t, info.IsDir(), "Case %d", i)
   459  		}
   460  
   461  		for k, v := range currCase.wantContent {
   462  			bytes, err := ioutil.ReadFile(path.Join(repo, k))
   463  			require.NoError(t, err)
   464  			assert.Equal(t, v, string(bytes), "Case %d", i)
   465  		}
   466  	}
   467  }