Skip to content

Commit

Permalink
add expiry check for SpaceClient and HealthChecker
Browse files Browse the repository at this point in the history
  • Loading branch information
uwefreidank committed Oct 8, 2024
1 parent edba2fd commit 40c1380
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
8 changes: 8 additions & 0 deletions internal/cf/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ func NewSpaceClient(spaceGuid string, url string, username string, password stri
delete(clientCache, identifier)
isInCache = false
}
if cacheEntry.expiresAt.Before(time.Now()) {
delete(clientCache, identifier)
isInCache = false
}
}

if !isInCache {
Expand Down Expand Up @@ -230,6 +234,10 @@ func NewSpaceHealthChecker(spaceGuid string, url string, username string, passwo
delete(clientCache, identifier)
isInCache = false
}
if cacheEntry.expiresAt.Before(time.Now()) {
delete(clientCache, identifier)
isInCache = false
}
}

if !isInCache {
Expand Down
32 changes: 32 additions & 0 deletions internal/cf/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,38 @@ var _ = Describe("CF Client tests", Ordered, func() {
Expect(server.ReceivedRequests()[3].URL.Path).To(Equal(instancesURI))
})

It("should re-create the refresh token after configured RefreshTokenAutoRenewalInterval", func() {
cfgZero := config.Config{
RefreshTokenAutoRenewalInterval: time.Duration(time.Second * 0),
}
spaceClient, err := NewSpaceClient(OrgName, url, Username, Password, &cfgZero)
Expect(err).To(BeNil())

spaceClient.GetInstance(ctx, map[string]string{"owner": Owner})
spaceClient, err = NewSpaceClient(OrgName, url, Username, Password, &cfgZero)
Expect(err).To(BeNil())
spaceClient.GetInstance(ctx, map[string]string{"owner": Owner})

Expect(server.ReceivedRequests()).To(HaveLen(6))

// Discover UAA endpoint
Expect(server.ReceivedRequests()[0].Method).To(Equal("GET"))
Expect(server.ReceivedRequests()[0].URL.Path).To(Equal("/"))
// Get new oAuth token
Expect(server.ReceivedRequests()[1].Method).To(Equal("POST"))
Expect(server.ReceivedRequests()[1].URL.Path).To(Equal(uaaURI))
// Get instance
Expect(server.ReceivedRequests()[2].Method).To(Equal("GET"))
Expect(server.ReceivedRequests()[2].URL.Path).To(Equal(instancesURI))

// Get new oAuth token
Expect(server.ReceivedRequests()[4].Method).To(Equal("POST"))
Expect(server.ReceivedRequests()[4].URL.Path).To(Equal(uaaURI))
// Get instance
Expect(server.ReceivedRequests()[5].Method).To(Equal("GET"))
Expect(server.ReceivedRequests()[5].URL.Path).To(Equal(instancesURI))
})

It("should be able to query two different spaces", func() {
// test space 1
spaceClient1, err1 := NewSpaceClient(SpaceName, url, Username, Password, &cfg)
Expand Down

0 comments on commit 40c1380

Please sign in to comment.