Skip to content

Commit

Permalink
Add it tests (#20)
Browse files Browse the repository at this point in the history
* Fixed setting of passwords in replications and remote repository
* Added integration tests
* Bumped to terraform v0.11.11
* Cleaned up lint checks and build
  • Loading branch information
dillon-giacoppo authored Jan 22, 2019
1 parent 7a6189c commit 23bfa93
Show file tree
Hide file tree
Showing 46 changed files with 780 additions and 400 deletions.
44 changes: 16 additions & 28 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,33 +1,21 @@
# Adapted from https://gist.github.com/y0ssar1an/df2dab474520c4086926f672c52db139
language: go
services:
- docker

# Only the last two Go releases are supported by the Go team with security
# updates. Any versions older than that should be considered deprecated.
# Don't bother testing with them. tip builds your code with the latest
# development version of Go. This can warn you that your code will break
# in the next version of Go. Don't worry! Later we declare that test runs
# are allowed to fail on Go tip.
language: go
go:
- 1.10.x
- 1.11.x
- master

matrix:
# It's ok if our code fails on unstable development versions of Go.
allow_failures:
- go: master
# Don't wait for tip tests to finish. Mark the test run green if the
# tests pass on the stable versions of Go.
fast_finish: true

# Don't email me the results of the test runs.
notifications:
email: false

- "1.11.x"
env:
- GO111MODULE="on"
GOFLAGS=-mod=vendor

install:
# Decrypt Artifactory License Key
- openssl aes-256-cbc -K $encrypted_1d073d5eb2c7_key -iv $encrypted_1d073d5eb2c7_iv
-in scripts/artifactory.lic.enc -out scripts/artifactory.lic -d

script:
- go build -v
- go test -v -race ./... # Run all the tests with the race detector enabled
- go vet ./... # go vet is the official Go static analyzer
- make testacc

matrix:
fast_finish: true
allow_failures:
- go: tip
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## 1.4.3 (January 22, 2019)
BUG FIXES:
* Fixed setting of passwords in replications and remote repository

NOTES:
* Added integration tests
* Bumped to terraform v0.11.11
* Cleaned up lint checks and build

## 1.4.2 (December 4, 2018)
FEATURES:
* Added docs website [#14]
Expand Down
29 changes: 29 additions & 0 deletions GNUmakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
TEST?=./...
GOFMT_FILES?=$$(find . -name '*.go' |grep -v vendor)
PKG_NAME=pkg/artifactory

default: build

build: fmtcheck
go install

test: fmtcheck
go test $(TEST) -timeout=30s -parallel=4

docker:
@echo "==> Launching Artifactory in Docker..."
@scripts/run-artifactory.sh

testacc: fmtcheck docker
TF_ACC=1 ARTIFACTORY_USERNAME=admin ARTIFACTORY_PASSWORD=password ARTIFACTORY_URL=http://localhost:8080/artifactory \
go test $(TEST) -v -parallel 20 $(TESTARGS) -timeout 120m
@docker stop artifactory

fmt:
@echo "==> Fixing source code with gofmt..."
gofmt -s -w ./$(PKG_NAME)

fmtcheck:
@sh -c "'$(CURDIR)/scripts/gofmtcheck.sh'"

.PHONY: build test testacc fmt
12 changes: 3 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# Terraform Provider Artifactory #
[![Build Status](https://travis-ci.org/atlassian/terraform-provider-artifactory.svg?branch=master)](https://travis-ci.org/atlassian/terraform-provider-artifactory)
[![Go Report Card](https://goreportcard.com/badge/github.com/atlassian/terraform-provider-artifactory)](https://goreportcard.com/report/github.com/atlassian/terraform-provider-artifactory)
## Using the provider ##

If you're building the provider, follow the instructions to [install it as a plugin.](https://www.terraform.io/docs/plugins/basics.html#installing-a-plugin) After placing it into your plugins directory, run `terraform init` to initialize it.

## Requirements ##
- [Go](https://golang.org/doc/install) 1.10+ (to build the provider plugin)
- [Go](https://golang.org/doc/install) 1.11+ (to build the provider plugin)
- [Terraform](https://www.terraform.io/downloads.html) 0.11

## Building The Provider ##
Expand All @@ -24,14 +26,6 @@ cd $GOPATH/src/github.com/atlassian/terraform-provider-artifactory
go install
```

## Roadmap ##

This library is being initially developed for an internal application at
Atlassian, so resources will likely be implemented in the order that they are
needed. Eventually, it would be ideal to cover the entire Artifactory API, so
contributions are of course always welcome. The calling pattern is pretty well
established, so adding new methods is relatively straightforward.

## Versioning ##

In general, this project follows [semver](https://semver.org/) as closely as we
Expand Down
2 changes: 1 addition & 1 deletion docs/r/remote_repository.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Arguments have a one to one mapping with the [JFrog API](https://www.jfrog.com/c
* `max_unique_snapshots` - (Optional)
* `suppress_pom_consistency_checks` - (Optional)
* `username` - (Optional)
* `password` - (Optional)
* `password` - (Optional) Requires password encryption to be turned off `POST /api/system/decrypt`
* `proxy` - (Optional)
* `hard_fail` - (Optional)
* `offline` - (Optional)
Expand Down
2 changes: 1 addition & 1 deletion docs/r/replication_config.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ The following arguments are supported:
* `url` - (Required)
* `socket_timeout_millis` - (Optional)
* `username` - (Optional)
* `password` - (Optional)
* `password` - (Optional) Requires password encryption to be turned off `POST /api/system/decrypt`
* `enabled` - (Optional)
* `sync_deletes` - (Optional)
* `sync_properties` - (Optional)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ require (
github.com/hashicorp/hcl2 v0.0.0-20180801154631-77c0b55a597c // indirect
github.com/hashicorp/hil v0.0.0-20170627220502-fa9f258a9250 // indirect
github.com/hashicorp/logutils v0.0.0-20150609070431-0dc08b1671f3 // indirect
github.com/hashicorp/terraform v0.11.7
github.com/hashicorp/terraform v0.11.11
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb // indirect
github.com/jtolds/gls v4.2.1+incompatible // indirect
github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ github.com/hashicorp/hil v0.0.0-20170627220502-fa9f258a9250 h1:fooK5IvDL/KIsi4Lx
github.com/hashicorp/hil v0.0.0-20170627220502-fa9f258a9250/go.mod h1:KHvg/R2/dPtaePb16oW4qIyzkMxXOL38xjRN64adsts=
github.com/hashicorp/logutils v0.0.0-20150609070431-0dc08b1671f3 h1:oD64EFjELI9RY9yoWlfua58r+etdnoIC871z+rr6lkA=
github.com/hashicorp/logutils v0.0.0-20150609070431-0dc08b1671f3/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
github.com/hashicorp/terraform v0.11.7 h1:NC3flYWAINcKyjTRIyU/Sz6kZLuSdAGEpqoyurZxLN0=
github.com/hashicorp/terraform v0.11.7/go.mod h1:uN1KUiT7Wdg61fPwsGXQwK3c8PmpIVZrt5Vcb1VrSoM=
github.com/hashicorp/terraform v0.11.11 h1:5q1y/a0RB1QmKc1n6E9tnWQqPMb+nEb7Bfol74N2grw=
github.com/hashicorp/terraform v0.11.11/go.mod h1:uN1KUiT7Wdg61fPwsGXQwK3c8PmpIVZrt5Vcb1VrSoM=
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb h1:b5rjCoWHc7eqmAS4/qyk21ZsHyb6Mxv/jykxvNTkU4M=
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8 h1:12VvqtR6Aowv3l/EQUlocDHW2Cp4G9WJVH7uyH8QFJE=
Expand Down
34 changes: 21 additions & 13 deletions pkg/artifactory/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package artifactory

import (
"fmt"
"net/http"

"github.com/atlassian/go-artifactory/pkg/artifactory"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
)

// Artifactory Provider that supports configuration via username+password or a token
// Supported resources are repos, users, groups, replications, and permissions
func Provider() terraform.ResourceProvider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -55,20 +58,25 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
return nil, fmt.Errorf("url cannot be nil")
}

if token, ok := d.GetOkExists("token"); ok {
tp := artifactory.TokenAuthTransport{
Token: token.(string),
}
return artifactory.NewClient(d.Get("url").(string), tp.Client())
} else if username, ok := d.GetOkExists("username"); !ok {
return nil, fmt.Errorf("error: Missing token and username. One must be set")
} else if password, ok := d.GetOkExists("password"); !ok {
return nil, fmt.Errorf("error: Basic auth used but password not set")
} else {
username := d.Get("username").(string)
password := d.Get("password").(string)
token := d.Get("token").(string)

var client *http.Client
if username != "" && password != "" {
tp := artifactory.BasicAuthTransport{
Username: username.(string),
Password: password.(string),
Username: username,
Password: password,
}
return artifactory.NewClient(d.Get("url").(string), tp.Client())
client = tp.Client()
} else if token != "" {
tp := &artifactory.TokenAuthTransport{
Token: token,
}
client = tp.Client()
} else {
return nil, fmt.Errorf("either [username, password] or [token] must be set to use provider")
}

return artifactory.NewClient(d.Get("url").(string), client)
}
12 changes: 6 additions & 6 deletions pkg/artifactory/resource_artifactory_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ func unmarshalGroup(s *schema.ResourceData) (*artifactory.Group, error) {

group := new(artifactory.Group)

group.Name = d.GetStringRef("name")
group.Description = d.GetStringRef("description")
group.AutoJoin = d.GetBoolRef("auto_join")
group.AdminPrivileges = d.GetBoolRef("admin_privileges")
group.Realm = d.GetStringRef("realm")
group.RealmAttributes = d.GetStringRef("realm_attributes")
group.Name = d.getStringRef("name")
group.Description = d.getStringRef("description")
group.AutoJoin = d.getBoolRef("auto_join")
group.AdminPrivileges = d.getBoolRef("admin_privileges")
group.Realm = d.getStringRef("realm")
group.RealmAttributes = d.getStringRef("realm_attributes")

// Validator
if group.AdminPrivileges != nil && group.AutoJoin != nil && *group.AdminPrivileges && *group.AutoJoin {
Expand Down
8 changes: 4 additions & 4 deletions pkg/artifactory/resource_artifactory_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"net/http"
)

const group_basic = `
const groupBasic = `
resource "artifactory_group" "test-group" {
name = "terraform-group"
}`
Expand All @@ -23,7 +23,7 @@ func TestAccGroup_basic(t *testing.T) {
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: group_basic,
Config: groupBasic,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("artifactory_group.test-group", "name", "terraform-group"),
),
Expand All @@ -32,7 +32,7 @@ func TestAccGroup_basic(t *testing.T) {
})
}

const group_full = `
const groupFull = `
resource "artifactory_group" "test-group" {
name = "terraform-group"
description = "Test group"
Expand All @@ -49,7 +49,7 @@ func TestAccGroup_full(t *testing.T) {
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: group_full,
Config: groupFull,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("artifactory_group.test-group", "name", "terraform-group"),
resource.TestCheckResourceAttr("artifactory_group.test-group", "auto_join", "true"),
Expand Down
46 changes: 23 additions & 23 deletions pkg/artifactory/resource_artifactory_local_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,28 +136,28 @@ func unmarshalLocalRepository(s *schema.ResourceData) *artifactory.LocalReposito

repo.RClass = artifactory.String("local")

repo.Key = d.GetStringRef("key")
repo.PackageType = d.GetStringRef("package_type")
repo.Description = d.GetStringRef("description")
repo.Notes = d.GetStringRef("notes")
repo.DebianTrivialLayout = d.GetBoolRef("debian_trivial_layout")
repo.IncludesPattern = d.GetStringRef("includes_pattern")
repo.ExcludesPattern = d.GetStringRef("excludes_pattern")
repo.RepoLayoutRef = d.GetStringRef("repo_layout_ref")
repo.MaxUniqueTags = d.GetIntRef("max_unique_tags")
repo.BlackedOut = d.GetBoolRef("blacked_out")
repo.CalculateYumMetadata = d.GetBoolRef("calculate_yum_metadata")
repo.YumRootDepth = d.GetIntRef("yum_root_depth")
repo.ArchiveBrowsingEnabled = d.GetBoolRef("archive_browsing_enabled")
repo.DockerApiVersion = d.GetStringRef("docker_api_verision")
repo.EnableFileListsIndexing = d.GetBoolRef("enable_file_lists_indexing")
repo.PropertySets = d.GetSetRef("property_sets")
repo.HandleReleases = d.GetBoolRef("handle_releases")
repo.HandleSnapshots = d.GetBoolRef("handle_snapshots")
repo.ChecksumPolicyType = d.GetStringRef("checksum_policy_type")
repo.MaxUniqueSnapshots = d.GetIntRef("max_unique_snapshots")
repo.SnapshotVersionBehavior = d.GetStringRef("snapshot_version_behavior")
repo.SuppressPomConsistencyChecks = d.GetBoolRef("suppress_pom_consistency_checks")
repo.Key = d.getStringRef("key")
repo.PackageType = d.getStringRef("package_type")
repo.Description = d.getStringRef("description")
repo.Notes = d.getStringRef("notes")
repo.DebianTrivialLayout = d.getBoolRef("debian_trivial_layout")
repo.IncludesPattern = d.getStringRef("includes_pattern")
repo.ExcludesPattern = d.getStringRef("excludes_pattern")
repo.RepoLayoutRef = d.getStringRef("repo_layout_ref")
repo.MaxUniqueTags = d.getIntRef("max_unique_tags")
repo.BlackedOut = d.getBoolRef("blacked_out")
repo.CalculateYumMetadata = d.getBoolRef("calculate_yum_metadata")
repo.YumRootDepth = d.getIntRef("yum_root_depth")
repo.ArchiveBrowsingEnabled = d.getBoolRef("archive_browsing_enabled")
repo.DockerApiVersion = d.getStringRef("docker_api_verision")
repo.EnableFileListsIndexing = d.getBoolRef("enable_file_lists_indexing")
repo.PropertySets = d.getSetRef("property_sets")
repo.HandleReleases = d.getBoolRef("handle_releases")
repo.HandleSnapshots = d.getBoolRef("handle_snapshots")
repo.ChecksumPolicyType = d.getStringRef("checksum_policy_type")
repo.MaxUniqueSnapshots = d.getIntRef("max_unique_snapshots")
repo.SnapshotVersionBehavior = d.getStringRef("snapshot_version_behavior")
repo.SuppressPomConsistencyChecks = d.getBoolRef("suppress_pom_consistency_checks")

return repo
}
Expand Down Expand Up @@ -200,7 +200,7 @@ func resourceLocalRepositoryRead(d *schema.ResourceData, m interface{}) error {
d.Set("yum_root_depth", repo.YumRootDepth)
d.Set("docker_api_version", repo.DockerApiVersion)
d.Set("enable_file_lists_indexing", repo.EnableFileListsIndexing)
d.Set("property_sets", schema.NewSet(schema.HashString, CastToInterfaceArr(*repo.PropertySets)))
d.Set("property_sets", schema.NewSet(schema.HashString, castToInterfaceArr(*repo.PropertySets)))
d.Set("handle_releases", repo.HandleReleases)
d.Set("handle_snapshots", repo.HandleSnapshots)
d.Set("checksum_policy_type", repo.ChecksumPolicyType)
Expand Down
8 changes: 4 additions & 4 deletions pkg/artifactory/resource_artifactory_local_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"net/http"
)

const localRepository_basic = `
const localRepositoryBasic = `
resource "artifactory_local_repository" "terraform-local-test-repo-basic" {
key = "terraform-local-test-repo-basic"
package_type = "docker"
Expand All @@ -24,7 +24,7 @@ func TestAccLocalRepository_basic(t *testing.T) {
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: localRepository_basic,
Config: localRepositoryBasic,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("artifactory_local_repository.terraform-local-test-repo-basic", "key", "terraform-local-test-repo-basic"),
resource.TestCheckResourceAttr("artifactory_local_repository.terraform-local-test-repo-basic", "package_type", "docker"),
Expand All @@ -34,7 +34,7 @@ func TestAccLocalRepository_basic(t *testing.T) {
})
}

const localRepositoryConfig_full = `
const localRepositoryConfigFull = `
resource "artifactory_local_repository" "terraform-local-test-repo-full" {
key = "terraform-local-test-repo-full"
package_type = "npm"
Expand Down Expand Up @@ -66,7 +66,7 @@ func TestAccLocalRepository_full(t *testing.T) {
CheckDestroy: resourceLocalRepositoryCheckDestroy("artifactory_local_repository.terraform-local-test-repo-full"),
Steps: []resource.TestStep{
{
Config: localRepositoryConfig_full,
Config: localRepositoryConfigFull,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("artifactory_local_repository.terraform-local-test-repo-full", "key", "terraform-local-test-repo-full"),
resource.TestCheckResourceAttr("artifactory_local_repository.terraform-local-test-repo-full", "package_type", "npm"),
Expand Down
Loading

0 comments on commit 23bfa93

Please sign in to comment.