Skip to content

Commit 7253ab5

Browse files
authored
Merge pull request #468 from cloudfoundry/user-tests
User missing tests from prior PRs and fix a few lint errors
2 parents 049e08a + 72006ef commit 7253ab5

File tree

9 files changed

+51
-28
lines changed

9 files changed

+51
-28
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
- name: Set up Go 1.x
2121
uses: actions/setup-go@v2
2222
with:
23-
go-version: ^1.16
23+
go-version: ^1.23
2424
- name: Run Test
2525
run: make test
2626
shell: bash

client/filter.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ func (r FilterModifier) String() string {
2626
return "lt"
2727
case FilterModifierLessThanOrEqual:
2828
return "lte"
29+
default:
30+
return ""
2931
}
30-
return ""
3132
}
3233

3334
type TimestampFilterList []TimestampFilter
@@ -124,7 +125,7 @@ type ExclusionFilter struct {
124125
}
125126

126127
func (e *ExclusionFilter) NotEqualTo(v ...string) {
127-
e.Filter.Values = v
128+
e.Values = v
128129
e.Not = true
129130
}
130131

client/polling.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"time"
77
)
88

9-
var AsyncProcessTimeoutError = errors.New("timed out after waiting for async process")
9+
var ErrAsyncProcessTimeout = errors.New("timed out after waiting for async process")
1010

1111
type PollingOptions struct {
1212
Timeout time.Duration
@@ -36,7 +36,7 @@ func PollForStateOrTimeout(getState getStateFunc, successState string, opts *Pol
3636
for {
3737
select {
3838
case <-timeout:
39-
return AsyncProcessTimeoutError
39+
return ErrAsyncProcessTimeout
4040
case <-ticker.C:
4141
state, cfError, err := getState()
4242
if err != nil {

client/polling_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,5 @@ func TestPollForStateOrTimeout(t *testing.T) {
3939
require.NoError(t, err)
4040

4141
err = PollForStateOrTimeout(timeoutFn, "SUCCESS", noWaitOpts)
42-
require.Equal(t, AsyncProcessTimeoutError, err)
42+
require.Equal(t, ErrAsyncProcessTimeout, err)
4343
}

client/user.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (c *UserClient) Create(ctx context.Context, r *resource.UserCreate) (*resou
4646
return c.createUserCall(ctx, r)
4747
}
4848

49-
// Create a new user via Username and Origin
49+
// CreateWithUsername creates a new user via Username and Origin
5050
func (c *UserClient) CreateWithUsername(ctx context.Context, r *resource.UserCreateWithUsername) (*resource.User, error) {
5151
return c.createUserCall(ctx, r)
5252
}

client/user_test.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,25 @@ func TestUsers(t *testing.T) {
2626
},
2727
Expected: user,
2828
Action: func(c *Client, t *testing.T) (any, error) {
29-
r := &resource.UserCreate{
30-
GUID: "3ebeaa8b-fd55-4724-a764-9f2231d8f7db",
31-
}
29+
r := resource.NewUserCreateWithGUID("3ebeaa8b-fd55-4724-a764-9f2231d8f7db")
3230
return c.Users.Create(context.Background(), r)
3331
},
3432
},
33+
{
34+
Description: "Create UAA shadow user",
35+
Route: testutil.MockRoute{
36+
Method: "POST",
37+
Endpoint: "/v3/users",
38+
Output: g.Single(user),
39+
Status: http.StatusCreated,
40+
PostForm: `{ "username": "sneal", "origin": "ldap" }`,
41+
},
42+
Expected: user,
43+
Action: func(c *Client, t *testing.T) (any, error) {
44+
r := resource.NewUserCreateWithUsername("sneal", "ldap")
45+
return c.Users.CreateWithUsername(context.Background(), r)
46+
},
47+
},
3548
{
3649
Description: "Delete user",
3750
Route: testutil.MockRoute{

operation/push.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package operation
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"io"
78
"os"
@@ -73,10 +74,10 @@ func (p *AppPushOperation) pushWithStrategyApp(ctx context.Context, space *resou
7374

7475
func (p *AppPushOperation) pushBlueGreenApp(ctx context.Context, space *resource.Space, manifest *AppManifest, zipFile io.Reader) (*resource.App, error) {
7576
originalApp, err := p.findApp(ctx, manifest.Name, space)
76-
if err != nil && err != client.ErrExactlyOneResultNotReturned {
77+
if err != nil && !errors.Is(err, client.ErrExactlyOneResultNotReturned) {
7778
return nil, err
7879
}
79-
if err == client.ErrExactlyOneResultNotReturned || originalApp.State != "STARTED" {
80+
if errors.Is(err, client.ErrExactlyOneResultNotReturned) || originalApp.State != "STARTED" {
8081
return p.pushApp(ctx, space, manifest, zipFile)
8182
}
8283

@@ -96,7 +97,7 @@ func (p *AppPushOperation) pushBlueGreenApp(ctx context.Context, space *resource
9697
Name: tempAppName,
9798
})
9899
if err != nil {
99-
return nil, fmt.Errorf("failed to update app name failed with: %s", err.Error())
100+
return nil, fmt.Errorf("failed to update app name failed with: %w", err)
100101
}
101102

102103
// Apply the manifest
@@ -107,23 +108,23 @@ func (p *AppPushOperation) pushBlueGreenApp(ctx context.Context, space *resource
107108
Name: originalApp.Name,
108109
})
109110
if err != nil {
110-
return nil, fmt.Errorf("failed to update app name back to original name: failed with %s", err.Error())
111+
return nil, fmt.Errorf("failed to update app name back to original name: failed with %w", err)
111112
}
112-
return nil, fmt.Errorf("blue green deployment failed with: %s", err.Error())
113+
return nil, fmt.Errorf("blue green deployment failed with: %w", err)
113114
}
114115
if newApp.State == "STARTED" {
115116
err = p.gracefulDeletion(ctx, originalApp)
116117
return newApp, err
117118
}
118-
return newApp, fmt.Errorf("failed to verify application start: %s", err.Error())
119+
return newApp, fmt.Errorf("failed to verify application start: %w", err)
119120
}
120121

121122
func (p *AppPushOperation) pushRollingApp(ctx context.Context, space *resource.Space, manifest *AppManifest, zipFile io.Reader) (*resource.App, error) {
122123
originalApp, err := p.findApp(ctx, manifest.Name, space)
123-
if err != nil && err != client.ErrExactlyOneResultNotReturned {
124+
if err != nil && !errors.Is(err, client.ErrExactlyOneResultNotReturned) {
124125
return nil, err
125126
}
126-
if err == client.ErrExactlyOneResultNotReturned || originalApp.State != "STARTED" {
127+
if errors.Is(err, client.ErrExactlyOneResultNotReturned) || originalApp.State != "STARTED" {
127128
return p.pushApp(ctx, space, manifest, zipFile)
128129
}
129130
// Get the fallback revision in case of rollback
@@ -151,7 +152,7 @@ func (p *AppPushOperation) pushRollingApp(ctx context.Context, space *resource.S
151152

152153
deployment, err := p.createNewDeployment(ctx, originalApp, droplet)
153154
if err != nil {
154-
return nil, fmt.Errorf("failed to deploy with: %s", err.Error())
155+
return nil, fmt.Errorf("failed to deploy with: %w", err)
155156
}
156157
// In case application crashed due to new deployment, deployment will be stuck with value "ACTIVE" and reason "DEPLOYING"
157158
// This will be considered as deployment failed after timeout
@@ -160,18 +161,18 @@ func (p *AppPushOperation) pushRollingApp(ctx context.Context, space *resource.S
160161
// Check the app state if app not started or deployment failed rollback the deployment
161162
originalApp, err = p.findApp(ctx, manifest.Name, space)
162163
if err != nil {
163-
return nil, fmt.Errorf("failed to verify application status with: %s", err.Error())
164+
return nil, fmt.Errorf("failed to verify application status with: %w", err)
164165
}
165166
if originalApp.State != "STARTED" || depPollErr != nil {
166167
rollBackDeployment, rollBackErr := p.rollBackDeployment(ctx, originalApp, fallbackRevision)
167168
if rollBackErr != nil {
168-
return nil, fmt.Errorf("failed to confirm rollback deployment with: %s", rollBackErr.Error())
169+
return nil, fmt.Errorf("failed to confirm rollback deployment with: %w", rollBackErr)
169170
}
170171
depRollPollErr := p.waitForDeployment(ctx, rollBackDeployment.GUID, *manifest.Instances)
171172
if depRollPollErr != nil {
172-
return nil, fmt.Errorf("failed to deploy with: %s \nfailed to confirm roll back to last deployment with: %s", depPollErr.Error(), depRollPollErr.Error())
173+
return nil, fmt.Errorf("failed to deploy with: %w \nfailed to confirm roll back to last deployment with: %w", depPollErr, depRollPollErr)
173174
}
174-
return nil, fmt.Errorf("failed to deploy with: %s \nrolled back to last deployment", depPollErr.Error())
175+
return nil, fmt.Errorf("failed to deploy with: %w \nrolled back to last deployment", depPollErr)
175176
}
176177

177178
return originalApp, nil
@@ -232,7 +233,7 @@ func (p *AppPushOperation) rollBackDeployment(ctx context.Context, originalApp *
232233
func (p *AppPushOperation) gracefulDeletion(ctx context.Context, app *resource.App) error {
233234
app, err := p.client.Applications.Stop(ctx, app.GUID)
234235
if err != nil {
235-
return fmt.Errorf("failed to stop the application with: %s", err.Error())
236+
return fmt.Errorf("failed to stop the application with: %w", err)
236237
}
237238
jobId, err := p.client.Applications.Delete(ctx, app.GUID)
238239
if err != nil {

resource/package.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,22 +131,24 @@ func (d *Package) UnmarshalJSON(data []byte) error {
131131
}
132132

133133
// post-processing based on type
134-
if d.Type == "bits" {
134+
switch d.Type {
135+
case "bits":
135136
var p BitsPackage
136137
err = json.Unmarshal(d.DataRaw, &p)
137138
if err != nil {
138139
return err
139140
}
140141
d.Data.Bits = &p
141142
return nil
142-
} else if d.Type == "docker" {
143+
case "docker":
143144
var p DockerPackage
144145
err = json.Unmarshal(d.DataRaw, &p)
145146
if err != nil {
146147
return err
147148
}
148149
d.Data.Docker = &p
149150
return nil
151+
default:
152+
return fmt.Errorf("could not unmarshal data as bits or docker package: %w", err)
150153
}
151-
return fmt.Errorf("could not unmarshal data as bits or docker package: %w", err)
152154
}

testutil/template/org_usage_summary.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
{
22
"usage_summary": {
33
"started_instances": 3,
4-
"memory_in_mb": 50
4+
"memory_in_mb": 50,
5+
"routes": 4,
6+
"service_instances": 2,
7+
"reserved_ports": 1,
8+
"domains": 4,
9+
"per_app_tasks": 2,
10+
"service_keys": 1
511
},
612
"links": {
713
"self": {

0 commit comments

Comments
 (0)