Skip to content

Commit

Permalink
Merge pull request #77 from getwilds/billing-stubs
Browse files Browse the repository at this point in the history
Billing stubs done
  • Loading branch information
sckott authored Oct 10, 2024
2 parents 32e63cd + 00f0410 commit 2abf7df
Show file tree
Hide file tree
Showing 5 changed files with 305 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ tests/fixtures/six_bucket*.yml
tests/fixtures/aws_policy_attach*.yml
tests/fixtures/aws_policy_detach*.yml
tests/fixtures/aws_policy_list_entities*.yml
tests/fixtures/billing*
tests/fixtures/blob*
tests/fixtures/*blob*
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ Suggests:
testthat (>= 3.0.0),
vcr (>= 0.6.0),
withr,
ggplot2
ggplot2,
webmockr
Remotes: ropensci/webmockr@including
Config/roxyglobals/filename: globals.R
Config/roxyglobals/unique: FALSE
Config/testthat/edition: 3
2 changes: 1 addition & 1 deletion R/constants.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# nolint start
MINIO_USER_PWD <- "minioadmin"
MINIO_ENDPOINT <- "http://127.0.0.1:9000"
LOCALSTACK_ENDPOINT <- "http://localhost.localstack.cloud:4566"
LOCALSTACK_ENDPOINT <- "http://localhost:4566"
LOCALSTACK_KEY <- "NOTAREALKEY"
LOCALSTACK_SECRET <- "AREALLYFAKETOKEN"
# nolint end
266 changes: 266 additions & 0 deletions tests/testthat/helper-stubs.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
# vcr::vcr_configure(dir = "tests/fixtures")

Check warning on line 1 in tests/testthat/helper-stubs.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/helper-stubs.R,line=1,col=3,[commented_code_linter] Commented code should be removed.
# cas <- vcr::use_cassette("billing_last_two_days", {
# library(lubridate)

Check warning on line 3 in tests/testthat/helper-stubs.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/helper-stubs.R,line=3,col=5,[commented_code_linter] Commented code should be removed.
# start_date <- today() - days(2)

Check warning on line 4 in tests/testthat/helper-stubs.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/helper-stubs.R,line=4,col=5,[commented_code_linter] Commented code should be removed.
# aws_billing(date_start = start_date)

Check warning on line 5 in tests/testthat/helper-stubs.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/helper-stubs.R,line=5,col=5,[commented_code_linter] Commented code should be removed.
# })

# cas_yaml <- yaml::yaml.load_file(cas$file())

Check warning on line 8 in tests/testthat/helper-stubs.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/helper-stubs.R,line=8,col=3,[commented_code_linter] Commented code should be removed.
# responses <- vapply(cas_yaml$http_interactions, \(w) w$response$body$string, "")

Check warning on line 9 in tests/testthat/helper-stubs.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/helper-stubs.R,line=9,col=3,[commented_code_linter] Commented code should be removed.

Check warning on line 9 in tests/testthat/helper-stubs.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/helper-stubs.R,line=9,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 82 characters.

# random_int <- function() {
# as.character(round(runif(1, 10^10, 10^12)))

Check warning on line 12 in tests/testthat/helper-stubs.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/helper-stubs.R,line=12,col=4,[commented_code_linter] Commented code should be removed.
# }

# library(glue)

Check warning on line 15 in tests/testthat/helper-stubs.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/helper-stubs.R,line=15,col=3,[commented_code_linter] Commented code should be removed.
# library(jqr)

Check warning on line 16 in tests/testthat/helper-stubs.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/helper-stubs.R,line=16,col=3,[commented_code_linter] Commented code should be removed.

# randomize_json <- function(json_str, which) {
# jq(json_str, ".DimensionValueAttributes[].Attributes.description |= \"some-org\"") %>%
# jq(glue(".DimensionValueAttributes[].Attributes.value |= \"{random_int()}\"")) %>%
# jq(glue(".DimensionValueAttributes[].Value |= \"{random_int()}\"")) %>%
# jq(glue(".ResultsByTime[].Groups[].Keys[1] |= \"{random_int()}\"")) %>%
# jq(glue(".ResultsByTime[].Groups[].Metrics.{which}.Amount |= \"{round(runif(1, 0, 10), digits = 8)}\""))
# }
# randomize_json(responses[1], "UnblendedCost") %>% jsonlite::prettify(indent = 2)
# randomize_json(responses[2], "BlendedCost") %>% jsonlite::prettify(indent = 2)

response_billing_unblended_1 <- '{
"DimensionValueAttributes": [
{
"Attributes": {
"description": "some-org",
"value": "946728054637"
},
"Value": "336503573358"
}
],
"GroupDefinitions": [
{
"Key": "SERVICE",
"Type": "DIMENSION"
},
{
"Key": "LINKED_ACCOUNT",
"Type": "DIMENSION"
}
],
"ResultsByTime": [
{
"Estimated": true,
"Groups": [
{
"Keys": [
"AWS Cost Explorer",
"224587672609"
],
"Metrics": {
"UnblendedCost": {
"Amount": "4.41095285",
"Unit": "USD"
}
}
},
{
"Keys": [
"AWS Secrets Manager",
"224587672609"
],
"Metrics": {
"UnblendedCost": {
"Amount": "4.41095285",
"Unit": "USD"
}
}
},
{
"Keys": [
"Amazon Simple Storage Service",
"224587672609"
],
"Metrics": {
"UnblendedCost": {
"Amount": "4.41095285",
"Unit": "USD"
}
}
}
],
"TimePeriod": {
"End": "2024-10-04",
"Start": "2024-10-03"
},
"Total": {
}
},
{
"Estimated": true,
"Groups": [
{
"Keys": [
"AWS Cost Explorer",
"224587672609"
],
"Metrics": {
"UnblendedCost": {
"Amount": "4.41095285",
"Unit": "USD"
}
}
},
{
"Keys": [
"AWS Secrets Manager",
"224587672609"
],
"Metrics": {
"UnblendedCost": {
"Amount": "4.41095285",
"Unit": "USD"
}
}
},
{
"Keys": [
"Amazon Simple Storage Service",
"224587672609"
],
"Metrics": {
"UnblendedCost": {
"Amount": "4.41095285",
"Unit": "USD"
}
}
}
],
"TimePeriod": {
"End": "2024-10-05",
"Start": "2024-10-04"
},
"Total": {
}
}
]
}'

response_billing_blended_1 <- '{
"DimensionValueAttributes": [
{
"Attributes": {
"description": "some-org",
"value": "787333819538"
},
"Value": "988949372892"
}
],
"GroupDefinitions": [
{
"Key": "SERVICE",
"Type": "DIMENSION"
},
{
"Key": "LINKED_ACCOUNT",
"Type": "DIMENSION"
}
],
"ResultsByTime": [
{
"Estimated": true,
"Groups": [
{
"Keys": [
"AWS Cost Explorer",
"62859488630"
],
"Metrics": {
"BlendedCost": {
"Amount": "7.11790631",
"Unit": "USD"
}
}
},
{
"Keys": [
"AWS Secrets Manager",
"62859488630"
],
"Metrics": {
"BlendedCost": {
"Amount": "7.11790631",
"Unit": "USD"
}
}
},
{
"Keys": [
"Amazon Simple Storage Service",
"62859488630"
],
"Metrics": {
"BlendedCost": {
"Amount": "7.11790631",
"Unit": "USD"
}
}
}
],
"TimePeriod": {
"End": "2024-10-04",
"Start": "2024-10-03"
},
"Total": {
}
},
{
"Estimated": true,
"Groups": [
{
"Keys": [
"AWS Cost Explorer",
"62859488630"
],
"Metrics": {
"BlendedCost": {
"Amount": "7.11790631",
"Unit": "USD"
}
}
},
{
"Keys": [
"AWS Secrets Manager",
"62859488630"
],
"Metrics": {
"BlendedCost": {
"Amount": "7.11790631",
"Unit": "USD"
}
}
},
{
"Keys": [
"Amazon Simple Storage Service",
"62859488630"
],
"Metrics": {
"BlendedCost": {
"Amount": "7.11790631",
"Unit": "USD"
}
}
}
],
"TimePeriod": {
"End": "2024-10-05",
"Start": "2024-10-04"
},
"Total": {
}
}
]
}'
32 changes: 32 additions & 0 deletions tests/testthat/test-billing.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
test_that("aws_billing", {
withr::with_package("webmockr", {
## Two stubs below are needed b/c aws_billing makes two http requests
## in one function call, for unblended and blended costs
stub_request("post", "https://ce.us-east-1.amazonaws.com") |>
wi_th(body = including(list(Metrics = list("UnblendedCost")))) |>
to_return(
body = jsonlite::minify(response_billing_unblended_1),
status = 200L,
headers = list("Content-type" = "application/x-amz-json-1.1")
)

stub_request("post", "https://ce.us-east-1.amazonaws.com") |>
wi_th(body = including(list(Metrics = list("BlendedCost")))) |>
to_return(
body = jsonlite::minify(response_billing_blended_1),
status = 200L,
headers = list("Content-type" = "application/x-amz-json-1.1")
)

enable(quiet = TRUE)

out <- aws_billing(date_start = "2024-10-03", date_end = "2024-10-05")

expect_s3_class(out, "tbl")
expect_equal(NCOL(out), 6)
expect_equal(sort(unique(out$id)), c("blended", "unblended"))

stub_registry_clear()
disable(quiet = TRUE)
})
})

0 comments on commit 2abf7df

Please sign in to comment.