-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #77 from getwilds/billing-stubs
Billing stubs done
- Loading branch information
Showing
5 changed files
with
305 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,266 @@ | ||
# vcr::vcr_configure(dir = "tests/fixtures") | ||
# cas <- vcr::use_cassette("billing_last_two_days", { | ||
# library(lubridate) | ||
# start_date <- today() - days(2) | ||
# aws_billing(date_start = start_date) | ||
# }) | ||
|
||
# cas_yaml <- yaml::yaml.load_file(cas$file()) | ||
# responses <- vapply(cas_yaml$http_interactions, \(w) w$response$body$string, "") | ||
Check warning on line 9 in tests/testthat/helper-stubs.R GitHub Actions / lint
|
||
|
||
# random_int <- function() { | ||
# as.character(round(runif(1, 10^10, 10^12))) | ||
# } | ||
|
||
# library(glue) | ||
# library(jqr) | ||
|
||
# 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": { | ||
} | ||
} | ||
] | ||
}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |